/**
 * project: mailfrills.com
 * filename: mailfrills.js
 *
 * @author jeffrey tan (jeffrey_design@yahoo.com)
 * @version 1.0 on aug 2008
 * @copyright jeffrey tan. all rights reserved.
 */

/* --------------------------------------->> common scripts <<--- */
/* common functions */
function setSpan(theSpan, theMsg){
  var errorMsg = "<font class='errortext'>" + theMsg + "</font>";
	document.getElementById(theSpan).innerHTML=errorMsg;
}

function clearSpan(theSpan, theMsg){
	document.getElementById(theSpan).innerHTML="";
}

function disableElements(theElement){
	theElement.disabled = true;
}

function enableElements(theElement){
	theElement.disabled = false;
}

/* function to the value of a string */
function trim(s) { // remove leading spaces and carriage returns
  while ((s.substring(0,1) == ' ') || (s.substring(0,1) == '\n') || (s.substring(0,1) == '\r')) { 
		s = s.substring(1,s.length);
	}

  // remove trailing spaces and carriage returns
  while ((s.substring(s.length-1,s.length) == ' ') || (s.substring(s.length-1,s.length) == '\n') || (s.substring(s.length-1,s.length) == '\r')) {
		s = s.substring(0,s.length-1); 
	}

	return s;
}

/* function to if the value of a string is numeric */
function IsNumeric(sText) {
	var ValidChars = "+-0123456789 ";
  var IsNumber = true;
  var Char;

	for (i = 0; i < sText.length && IsNumber; i++) {
  	Char = sText.charAt(i); 
    if (ValidChars.indexOf(Char) == -1) IsNumber = false;
  }
  return IsNumber;
}

/* function to check if the value of a string is alphanumeric */
function IsAlphanumeric(sText) {
	/* var InvalidChars = "_:;?=+(){}[]/\|`!@#$%^*~<>"; */
	var InvalidChars = "_:;?={}[]/\|`!@$%^*~<>";
  var IsAlphanumeric = true;
  var Char;

  for (i=0; i<sText.length && IsAlphanumeric; i++) { 
  	Char = sText.charAt(i);
    if (InvalidChars.indexOf(Char) != -1) IsAlphanumeric = false;
  }
  return IsAlphanumeric;
}

/* function to check if the value of a string is alphabetic */
function IsAlphabetic(sText) {
	var InvalidChars = "0123456789-._:;,?=+(){}[]/\|!@#$%^&*~<>";
	//var InvalidChars = "0123456789_:;,?=+(){}[]/\|!@#$%^&*~<>";
  var IsAlpha = true;
  var Char;

  for (i=0; i<sText.length && IsAlpha; i++) { 
  	Char = sText.charAt(i);
    if (InvalidChars.indexOf(Char) != -1) IsAlpha = false;
  }
  return IsAlpha;
}

/* function to popup a centralised window with varying width and height */
function popupwin(url, win, w, h) {
	var features, leftValue, topValue;
  var width = w;
  var height = h;

  leftValue = (screen.availWidth)/2 - (width/2);
  topValue = (screen.availHeight)/2 - (height/2);

  features = "width=" + width + ", "
  	+ "height=" + height + ", "
    + "left=" + leftValue + ", "
    + "top=" + topValue + ", "
    + "directories=no, location=no, menubar=no, scrollbars=yes, status=no, toolbar=no, resizable=yes";

	previewWin = window.open(url, win, features);     // show browser window
	previewWin.focus();    														// bring minimise window back on focus
}

/* function to verify given email address */
function isValidEmail(theEmail){
	var atPos, dotPos
	
	atPos = theEmail.indexOf("@",1)
	if (atPos == -1) return false;	
	dotPos = theEmail.indexOf(".",atPos)
	if (dotPos == -1) return false;
	if (dotPos+2 > theEmail.length) return false
	
	return true;
}

function toggleClass (theID, theClass){
	var handler = document.getElementById(theID);
	handler.className=theClass;
}

/* --------------------------------------->> common scripts <<--- */

/* --------------------------------------->> mailfrills signup form <<--- */
/* checkSignupForm */
function checkSignupForm(theForm){
  disableElements(theForm.submitButton);
  
	/* check: full name */
	if(theForm.full_name.value == ""){
    setSpan('full_name_span', "Please enter Full Name.");    
		theForm.full_name.focus();
		enableElements(theForm.submitButton);
    return false;
  } else if(!IsAlphabetic(theForm.full_name.value)){
	  setSpan('full_name_span', "Full Name contains invalid characters. <br>\(Only alphabets allowed\)");
		theForm.full_name.focus();
		enableElements(theForm.submitButton);
		return false;
	} else {
		clearSpan('full_name_span', "");
	}
	
	/* check: email */
	if(theForm.email.value == ""){
    setSpan('email_span', "Please enter Email.");
		theForm.email.focus();
		enableElements(theForm.submitButton);
    return false;
  } else if(!isValidEmail(theForm.email.value)){
	  setSpan('email_span', "Invalid email format.");
		theForm.email.focus();
		enableElements(theForm.submitButton);
		return false;
	} else {
		clearSpan('email_span', "");
	}
	
	/* check: company name */
	if(theForm.company_name.value == ""){
    setSpan('company_name_span', "Please enter Company Name.");    
		theForm.company_name.focus();
		enableElements(theForm.submitButton);
    return false;
  } else if(!IsAlphanumeric(theForm.company_name.value)){
	  setSpan('company_name_span', "Company Name contains invalid characters. <br>\(Only alphabets & numeric allowed\)");
		theForm.company_name.focus();
		enableElements(theForm.submitButton);
		return false;
	} else {
		clearSpan('company_name_span', "");
	}
	
	/* check: contact number */
	if(theForm.contact.value == ""){
    setSpan('contact_span', "Please enter Contact.");
		theForm.contact.focus();
		enableElements(theForm.submitButton);
    return false;
  } else if(!IsNumeric(theForm.contact.value)){
	  setSpan('contact_span', "Contact contains invalid characters. <br>\(Only numberical, '+' & '-' allowed\)");
		theForm.contact.focus();
		enableElements(theForm.submitButton);
		return false;
	} else {
		clearSpan('contact_span', "");
	}
	
	/* check: use_mailfrills */
	if(theForm.use_mailfrills.value == ""){
    setSpan('use_span', "Please indicate how do you intend to use mailfrills");
		theForm.use_mailfrills.focus();
		enableElements(theForm.submitButton);
    return false;
  } else {
		clearSpan('use_span', "");
	}
	
	if(theForm.anti_spam_policy.checked == false){
		setSpan('policy_span', "Please read our anti-spam policy");
		theForm.anti_spam_policy.focus();
		enableElements(theForm.submitButton);
    return false;
	} else {
		clearSpan('policy_span', "");
	}
	
}

/* --------------------------------------->> mailfrills signup form <<--- */


