/* Validation for create account */
function createAcc()
{
	if(document.form_create.email.value=="")
	{
		alert("Please enter your email address.");
		document.form_create.email.focus(); return false;
	}
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	if(reg.test(document.form_create.email.value) == false) 
	{
		alert('Invalid Email Address');
		document.form_create.email.focus(); return false;
	}
	if(document.form_create.password.value=="")
	{
		alert("Please enter you desire password.");
		document.form_create.password.focus(); return false;
	}
	if(document.form_create.foldername.value=="")
	{
		alert("Please enter you desire myurl. \nHint, you can choose your username as your myurl \nnext time when you access your custom search interface as \nhttp://cs.limcorp.net/myurl/.");
		document.form_create.foldername.focus(); return false;
	}
	
	var iChars = "~`!@#$%^&*()+=-[]{}\\|\'\";:,/<>?";
	for (var i = 0; i < document.form_create.foldername.value.length; i++) {
		if (iChars.indexOf(document.form_create.foldername.value.charAt(i)) != -1) {
			alert ("Found special character(s) at myurl,\n this is not allowed. Please use only digits, alphabets, underscode.");
			document.form_create.foldername.focus(); return false;
		}
	}

	if(document.form_create.turnNo.value=="")
	{
		alert("Please enter turning number.");
		document.form_create.turnNo.focus(); return false;
	}
	if(document.form_create.rand_code1.value!=document.form_create.turnNo.value)
	{
		alert("Invalid turning number.");
		document.form_create.turnNo.focus(); return false;
	}
	
	document.getElementById("buttonCreate").style.visibility='hidden';
}
/* Validation for forgotten password */
function reqPW()
{
	if(document.form_reqpw.email.value=="")
	{
		alert("Please enter your email address.");
		document.form_reqpw.email.focus(); return false;
	}
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	if(reg.test(document.form_reqpw.email.value) == false) 
	{
		alert('Invalid Email Address');
		document.form_reqpw.email.focus(); return false;
	}
	if(document.form_reqpw.turnNo.value=="")
	{
		alert("Please enter turning number.");
		document.form_reqpw.turnNo.focus(); return false;
	}
	if(document.form_reqpw.rand_code3.value!=document.form_reqpw.turnNo.value)
	{
		alert("Invalid turning number.");
		document.form_reqpw.turnNo.focus(); return false;
	}
	document.getElementById("buttonGo").style.visibility='hidden';
}

/* Validation for edit account */
function editAcc()
{
	if(document.form_edit.email.value=="")
	{
		alert("Please enter your email address.");
		document.form_edit.email.focus(); return false;
	}
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	if(reg.test(document.form_edit.email.value) == false) 
	{
		alert('Invalid Email Address');
		document.form_edit.email.focus(); return false;
	}
	if(document.form_edit.password.value=="")
	{
		alert("Please enter you desire password.");
		document.form_edit.password.focus(); return false;
	}
	if(document.form_edit.turnNo.value=="")
	{
		alert("Please enter turning number.");
		document.form_edit.turnNo.focus(); return false;
	}
	if(document.form_edit.rand_code2.value!=document.form_edit.turnNo.value)
	{
		alert("Invalid turning number.");
		document.form_edit.turnNo.focus(); return false;
	}
	document.getElementById("buttonEdit").style.visibility='hidden';
}
function customizeLayout()
{
	/* Check for file size and file type */
	if(document.form_edit.GoToSaveData.value==1)
	{
		cfs=window.open('','cfs','toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=no,width=400,height=600,left=430,top=23');
		document.form_edit.action = "checkFilesSize.php";
		document.form_edit.target = "cfs";
		document.form_edit.submit();
		return false;
	}
	
	/* Restore to default */
	if(document.form_edit.GoToSaveData.value==2)
	{
		var confirmation=confirm("Confirm to restore all setting to default? \nThis action can not be undo once restored.");
		if(confirmation)
		{
			document.form_edit.action = "restore.php";
			document.form_edit.target = "";
			document.form_edit.submit();
			return false;
		}
		else
		{
			return false;
		}
	}
	
	if(document.form_edit.password.value=="")
	{
		alert("Password field can not leave empty.");
		document.form_edit.password.focus(); return false;
	}
	if(document.form_edit.cpassword.value=="")
	{
		alert("Confirm password field can not leave empty.");
		document.form_edit.cpassword.focus(); return false;
	}
	if(document.form_edit.password.value!=document.form_edit.cpassword.value)
	{
		alert("Password and confirm password not match.");
		document.form_edit.password.value="";
		document.form_edit.cpassword.value="";
		document.form_edit.password.focus(); return false;
	}
	if(document.form_edit.csfile.value.length>2300)
	{
		alert(document.form_edit.csfile.value.length+" characters, Content in main.css file too heavy, max 2300 characters.");
		return false;
	}

	/* Save data */
	if(document.form_edit.GoToSaveData.value==0)
	{
		document.form_edit.action = "saveEdit.php";
		document.form_edit.target = "";
		document.form_edit.submit();
	}
}
/* Validation for edit account */
function deleteAcc()
{
	/* Delete all records */
	if(document.form_delete.email.value=="")
	{
		alert("Please enter email.");
		document.form_delete.email.focus(); return false;
	}
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	if(reg.test(document.form_delete.email.value) == false) 
	{
		alert('Invalid Email Address');
		document.form_delete.email.focus(); return false;
	}
	if(document.form_delete.password.value=="")
	{
		alert("Please enter password.");
		document.form_delete.password.focus(); return false;
	}
}
function IsNumeric(strString)
//  check for valid numeric strings	
{
	var strValidChars = "0123456789";
	var strChar;
	var blnResult = true;
	//if (field.value.length == 0) return false;
	//  test strString consists of valid characters listed above
	for (i = 0; i < strString.length && blnResult == true; i++)
	{
		strChar = strString.charAt(i);
		if (strValidChars.indexOf(strChar) == -1)
		{
			blnResult = false;
		}
	}
	return blnResult;
	
}
function digitsValidation(field,cntfield,maxlimit)
{
	try
	{
		if (field.value > maxlimit)
		{
			alert("Value larger than 800.")
			field.value=0;
			return false;
			//field.value = field.value.substring(0, maxlimit);
		}
		else if(!IsNumeric(field.value))
		{
			alert("Please enter numeric only.")
			field.value=0;
			return false;
		}
	}
	catch (E) { alert(E.message); }
}

/* Ajax delete images */
function deleteImage(fieldName, accID, imgPath)
{//alert(imgPath);
	if(fieldName=="logo") 
		http('POST','deleteImage.php',show_logoImage,'fieldName='+fieldName+'&accID='+accID+'&imgPath='+imgPath,false);
	else if(fieldName=="stretch") 
		http('POST','deleteImage.php',show_stretchImage,'fieldName='+fieldName+'&accID='+accID+'&imgPath='+imgPath,false);
	else if(fieldName=="tiled") 
		http('POST','deleteImage.php',show_tiledImage,'fieldName='+fieldName+'&accID='+accID+'&imgPath='+imgPath,false);
	else if(fieldName=="favicon") 
		http('POST','deleteImage.php',show_faviconImage,'fieldName='+fieldName+'&accID='+accID+'&imgPath='+imgPath,false);
	else if(fieldName=="frame") 
		http('POST','deleteImage.php',show_frameImage,'fieldName='+fieldName+'&accID='+accID+'&imgPath='+imgPath,false);
	else if(fieldName=="innerframe") 
		http('POST','deleteImage.php',show_innerframeImage,'fieldName='+fieldName+'&accID='+accID+'&imgPath='+imgPath,false);
}

function show_logoImage(obj)
{elm = document.getElementById('spanlogo');elm.innerHTML = obj['spanlogo'];}
function show_stretchImage(obj)
{elm = document.getElementById('spanstretch');elm.innerHTML = obj['spanstretch'];}
function show_tiledImage(obj)
{elm = document.getElementById('spantiled');elm.innerHTML = obj['spantiled'];}
function show_faviconImage(obj)
{elm = document.getElementById('spanfavicon');elm.innerHTML = obj['spanfavicon'];}
function show_frameImage(obj)
{elm = document.getElementById('spanframe');elm.innerHTML = obj['spanframe'];}
function show_innerframeImage(obj)
{elm = document.getElementById('spaninnerframe');elm.innerHTML = obj['spaninnerframe'];}



function contactus() {
	if(document.contactus_form.name.value=='') { alert("Please enter your Name."); document.contactus_form.name.focus(); return false; }			
	if(document.contactus_form.email.value=='') { alert("Please enter your Email."); document.contactus_form.email.focus(); return false; }			
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	if(reg.test(document.contactus_form.email.value) == false) 
	{alert('Invalid Email Address'); document.contactus_form.email.focus(); return false; }
	if(document.contactus_form.subject.value=='') 
	{ alert("Please enter Subject of email."); document.contactus_form.subject.focus(); return false; }			
	if(document.contactus_form.message.value=='') 
	{ alert("Please enter Message."); document.contactus_form.message.focus(); return false; }
	document.getElementById("buttonReset").style.visibility='hidden';
	document.getElementById("buttonSend").style.visibility='hidden';
}
