/*
------------------------------------------------------------------------
File: common.js
Desc: The file define all the javascript functions used in this system
Author: M.J.
Last Modified: 01/08/2005
------------------------------------------------------------------------
*/

/*
-------------------------------------------------------------------------
Function: VerifyPhotoFormat()
Description: Verify the photo format to be uploaded
--------------------------------------------------------------------------
*/
function VerifyPhotoFormat(id){
	var acceptTypes="jpg-jpeg-jpe-gif-png";
	var fileName=document.getElementById(id).value;
	var fileName=fileName.toLowerCase();
	
	if(fileName){
		var ext=fileName.substring(fileName.lastIndexOf(".")+1);
		if(acceptTypes.indexOf(ext)>=0){
			return true;
			}
		else{
			alert("You can only upload photos of .jpg, .jpeg, .jpe, .gif, .png extension(format)");
			return false;
		}
	}
	else
		return true;
}

/*
-------------------------------------------------------------------------
Function: VerifyFileFormat()
Description: Verify the file format to be uploaded
--------------------------------------------------------------------------
*/
function VerifyFileFormat(){
	var theForm=document.forms[0];
	var acceptTypes=null, theType=null;
	for( var i=0; i<theForm.fileType.length; i++){
		if(theForm.fileType[i].checked){
			theType=theForm.fileType[i].value;
			break;
			}
		}
	
	switch(theType){
		case 'photo':
			acceptTypes="jpg-jpeg-jpe-gif-png";
			errMsg="You can only upload photos of .jpg, .jpeg, .jpe, .gif, .png extension(format)";
			break;
		case 'video':
			acceptTypes="wmv-mpeg-avi-mpg";
			errMsg="You can only upload videos of .wmv, .mpeg, .avi extension(format)";
			break;
		case 'audio':
			acceptTypes="wav";
			errMsg="You can only upload audio files of .wav extension(format)";
			break;
		case 'file':
			acceptTypes="pdf-doc-txt";
			errMsg="You can only upload documents of .pdf, .doc, .txt extension(format)";
			break;
		default:
			break;
		}
	
	var fileName=document.getElementById('mediaFile').value;
	var fileName=fileName.toLowerCase();	

	if(fileName || document.getElementByName('ofile').value){
		var ext=fileName.substring(fileName.lastIndexOf(".")+1);
		if(acceptTypes.indexOf(ext)>=0){
			return true;
			}
		else{
			alert(errMsg);
			return false;
		}
	}
	else{
		alert('Pleae select a file to upload');
		return false;
		}
}

/*
-------------------------------------------------------------------------
Function: ToggleSection(sectionId1,sectionId2)
Description: Toggle between two section
--------------------------------------------------------------------------
*/
function ToggleSection(sectionId1,sectionId2){
	if(document.getElementById(sectionId1).style.display=='block'){
		document.getElementById(sectionId1).style.display='none';
		document.getElementById(sectionId2).style.display='block';
	}else{
		document.getElementById(sectionId1).style.display='block';
		document.getElementById(sectionId2).style.display='none';
	}
}

/*
-------------------------------------------------------------------------
Function: openWindow(winURL,winSetting)
Description: open up a new window.
--------------------------------------------------------------------------
*/
function openWindow(winURL,winName,winSetting){
	var newWin=window.open(winURL,winName,winSetting);
	newWin.focus();
	}

/*
-------------------------------------------------------------------------
Function: VerifyEmptyField()
Description: Use this function to verify the compulsory fields of a submit 
			 form, use unlimited arguments, which are the id of the form fields.
--------------------------------------------------------------------------
*/
function VerifyEmptyField(){
	var verified=true;
	for(i=0;i<arguments.length;i++){
		if(document.getElementById(arguments[i]).value==""){
			alert("The fields marked with * can not be empty!");
			verified=false;
			break;
		}
	}
	return verified;
}
			
/*
-------------------------------------------------------------------------
Function: VerifyPassword()
Description: Verify password for registration forms to see if both passwords
			 are the same.
--------------------------------------------------------------------------
*/
function VerifyPassword(password01,password02){
	var pass01=document.getElementById(password01);
	var pass02=document.getElementById(password02);
	if(pass01.value!=pass02.value){
		alert("Passwords entered are not the same, please retype them!");
		pass01.select();
		return false;
	}
	return true;
}

/*
-------------------------------------------------------------------------
Function: VerifyAgreement()
Description: Verify if the buyer has tick the checkbox of agreement when checkout.
--------------------------------------------------------------------------
*/
function VerifyAgreement(){
	var agreement=document.getElementById('agreement');
	if(agreement.checked!=true){
		alert("You must agree to the terms and condtions in order to proceed!");
		return false;
	}
	return true;
}

/*
-------------------------------------------------------------------------
Function: VerifyEmail()
Description: Verify email address
--------------------------------------------------------------------------
*/
function VerifyEmail(emailAddress){
	var re=/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
	if(re.test(document.getElementById(emailAddress).value))
		return true;
	alert("Please enter a valid email address!");
	document.getElementById(emailAddress).focus();
	document.getElementById(emailAddress).select();
	return false;
}

/*
-------------------------------------------------------------------------
Function: VerifyRegister()
Description: Use this function to verify the registration form of members in the front end
--------------------------------------------------------------------------
*/
function VerifyRegister(){
	var email=document.getElementById('email');
	var password=document.getElementById('password');
	var repassword=document.getElementById('repassword');
	var nickName=document.getElementById('nickName');
    if(email.value=="" || password.value=="" || repassword.value=="" || nickName==""){
			alert("The fields marked with * can not be empty!");
			return false;
	}
	if(password.value!=repassword.value){
			alert("The passwords entered are not the same");
			return false;
	}
	//test the email address
	re=/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
	if(!re.test(email.value)){
		alert("Pleaes enter a valid email address!");
		return false;
	}
	return true;
}
