/**
 *  For Login purpose
 *
 *  KC Lai 7/04/04
 */

function checklogin(form) {
	/* Added by KCLai 4/1/04 */
	result = validMailId(form.email.value);

		if (result == false )
		{
			alert("Invalid email ! Please key in a valid email address.");
			return false;
		}

		if (validateField(form.password, "Password") == false )
		{
			alert("Please key in your password.");
			return false;
		}


	/* End */

return true;
}

//spawn new window
function NewWindow(mypage, myname, w, h, scroll) {
	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;
	winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',resizable'
	win = window.open(mypage, myname, winprops)
	if ( parseInt(navigator.appVersion ) >= 4) { win.window.focus(); }
}

//client side form validation
function validateField(myfield,s) {
		//alert("Yap Chor !");

	var charexp = /./
		if (charexp.test(myfield.value))
		{
		//alert("Pass woh low yaw !");
		return true }
		else
		{
		myfield.focus()
		alert("The " + s + " field is blank.")
		return false
		}
}

function validateChar(myfield,s,cmax){
		if (myfield.value.length <= cmax)
		{ return true }
		else
		{
			myfield.focus()
			alert("The " + s + " field contains more then "+ cmax +" characters.")
			return false
		}
}

/*
 * Not Working
 */
function validateEmail(myfield, s){
	if (validateField(myfield,s)){
	var pattern = /^[a-z][a-z_0-9\.\-]+@[a-z_0-9\.\-]+\.[a-z_0-9\.\-]+/i
	if (pattern.test(myfield.value)){
		return true
	} else {
		myfield.focus()
		alert("The " + s + " field is not a valid email address.")
		return false
	}
	} else {
		return false
	}
}

function validateDate(myfield, s){
	if (validateField(myfield,s)){
	var pattern = /^[0-9]+\/[0-9]+\/[0-9]+/
	if (pattern.test(myfield.value)){
		return true
	} else {
		myfield.focus()
		alert("The " + s + " field is not a valid date.")
		return false
	}
	} else {
		return false
	}
}

// Menu Cell Color
function inCell(cell) {
	if (!cell.contains(event.fromElement)) {
		cell.bgColor = "#DDDDDD";
	}
}

function outCell(cell) {
	if (!cell.contains(event.toElement)) {
		cell.bgColor = "#FFFFFF";
	}
}

function openf()
{
  window.open("categoryselect.asp", 'windowname', 'height=200,width=500,scrollbars=yes');
}

function sendf()
{
//if you are working with frames you use the following command
//window.parent.opener.parent.FRAMENAME.myform.name.value
//change FRAMENAME in the name of your frame
  window.parent.opener.parent.myform.name.value = document.myform.names.value;
  window.parent.opener.parent.myform.fname.value = document.myform.names.options[document.myform.names.options.selectedIndex].text;
  this.close();
}

/**
 * This function is used to check if the given mailId is a proper mail id or not
 * @param mailId - String
 * @return boolean. true -- valid mail id, false -- not valid mail id.
 */
function validMailId(mailId)
{
 mailId = trim(mailId);

 ////alert("mail Id " + mailId);
 if (mailId.length <= 0) {
  //alert("invalid mail id -- Reason mailId is empty");
  return false;
 }

 if (mailId.indexOf("@") < 0 ) {
  //alert("invalid mail id -- Reason does not contain @");
  return false;
 }
 else if (mailId.indexOf(".") < 0 )
 {
  //alert("invalid mail id -- Reason does not contain dot .");
  return false;
 }
 if (mailId.indexOf("@") == 0 || mailId.lastIndexOf("@") == (mailId.length)-1 )
 {
  //alert("invalid mail id -- Reason starts with @ or ends with @");
  return false;
 }
 if (mailId.indexOf(".") == 0 || mailId.lastIndexOf(".") == (mailId.length)-1 )
 {
  //alert("invalid mail id -- Reason starts with dot . or ends with dot . ");
  return false;
 }
 // commented bcoz it should allow dot in email. eg aaa.bbb@yahoo.com
 //if (mailId.indexOf(".") < mailId.indexOf("@") )
 //{
 // return false;
 //}
 if (mailId.indexOf("@.") >= 0 || mailId.indexOf(".@") >= 0 )
 {
  //alert("invalid mail id -- Reason @. not possible");
  return false;
 }
 if (mailId.indexOf("..") >= 0 || mailId.indexOf("..") >= 0 )
 {
  //alert("invalid mail id -- Reason .. not possible");
  return false;
 }
 //to check if the mailId has the following character which is not allowed
 var charNotAllowed  = new String ("~!#$%^&*()+=;:<>/?{}[]|\\,'\"");
 for (var i=0;i<charNotAllowed.length;i++) {
  for(var j=0;j<mailId.length;j++)
  {
   //checks if whitespace is there.
   if(mailId.charAt(j)==' ')
   {
    //alert("invalid mail id -- Reason contains space");
    return false;
   }
   if(mailId.charAt(j)==charNotAllowed.charAt(i)){
    //alert("invalid mail id -- Reason contains not allowed characters " + charNotAllowed.charAt(i));
    return false;
   }
  }
 }
 //all checks complete
 return true;
}

/*  Trims the string passed
 *  Removes leading and trailing spaces from the passed string. Also removes
 *  consecutive spaces and replaces it with one space. If something besides
 *  a string is passed in (null, custom object, etc.) then return the input.
 *	-- A.Siva
 *  @param - field object
 */
function trim(inputString) {
   if (typeof inputString != "string") { return inputString; }

   if (inputString.length == 0) {
   		return inputString;
   }
   var retValue = inputString;

   var ch = retValue.substring(0, 1);
   while (ch == " ") { // Check for spaces at the beginning of the string
      retValue = retValue.substring(1, retValue.length);
      ch = retValue.substring(0, 1);
   }
   ch = retValue.substring(retValue.length-1, retValue.length);
   while (ch == " ") { // Check for spaces at the end of the string
      retValue = retValue.substring(0, retValue.length-1);
      ch = retValue.substring(retValue.length-1, retValue.length);
   }
   while (retValue.indexOf("  ") != -1) { // Note that there are two spaces in the string - look for multiple spaces within the string
      retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
   }
   return retValue; // Return the trimmed string back to the user
} // Ends the "trim" function

