//------------------------------------------------------------------------------------
//Validation for mandatory fields
//------------------------------------------------------------------------------------

function CheckMandatory(oForm, MandatoryString)
{
	var Completed = true;

	var temp = oForm.elements.length;
	
	//MandatoryString="Salutation~FirstName~LastName~Email~Company~Function~Username~Password"
	//alert(MandatoryString)
	MandatoryArray = MandatoryString.split("~");
	
	for(i = 0; i < temp; i++)
	{
		for (j = 0; j < MandatoryArray.length; j++)
		{
			//alert("Mandatory For loop " + oForm.elements[j].name)
			if (oForm.elements[i].name == MandatoryArray[j])
			{
				//alert("If Stmt " + oForm.elements[i].name)
				if (oForm.elements[i].value == "")
				{
					//alert("Null Value=" + oForm.elements[i].name)
					Completed = false;
				}
			}
		}
	}
	if (Completed == false)
	{
		alert ("Please give information marked as mandatory(*)!");
		return false;
	}
	else
		return true;
}

//------------------------------------------------------------------------------------
//Validation for email addresses
//------------------------------------------------------------------------------------
function emailcheck (str) {
	var at="@";
	var dot=".";
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);
	if (str.indexOf(at)==-1){
	   alert("Invalid E-mail Address");
	   return false;
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   alert("Invalid E-mail Address");
	   return false;
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		alert("Invalid E-mail Address");
		return false;
	}

	 if (str.indexOf(at,(lat+1))!=-1){
		alert("Invalid E-mail Address");
		return false;
	 }

	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		alert("Invalid E-mail Address");
		return false;
	 }

	 if (str.indexOf(dot,(lat+2))==-1){
		alert("Invalid E-mail Address");
		return false;
	 }
	
	 if (str.indexOf(" ")!=-1){
		alert("Invalid E-mail Address");
		return false;
	 }

	 return true;					
}

//------------------------------------------------------------------------------------
//validation for leading/endiing spaces
//------------------------------------------------------------------------------------
function trim(inputString) {
	// http://www.breakingpar.com/bkp/home.nsf/0/87256B14007C5C6A87256AFB0013C722
   // 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.
   if (typeof inputString != "string") { 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


function checkValue(eID) {
	var eVal = document.getElementById(eID).value;
	if (trim(eVal) == 'Name *' ){
		document.getElementById(eID).value = "";
	}
	if (trim(eVal) == 'E-mail *' ){
		document.getElementById(eID).value = "";
	}
}


// -----------
function ValidateSubscription()
{
	var oForm  = document.getElementById("NewsSubscription");
	var oEmail = document.getElementById("email");
    var dName  = document.getElementById("usrname");
    var MandatoryString, bValidEmail;
	var url;
    
    bValidEmail = false;
    MandatoryString = "usrname~email";
    
    if (trim(dName.value) != 'Name *' && (trim(oEmail.value) != 'E-mail *'))
    {
        if (CheckMandatory(oForm, MandatoryString) == true)
        {
            if (emailcheck(oEmail.value) == true)
			{
				oForm.hNewsAction.value = "Subscribe"
                bValidEmail = true;
				// alert(oForm.hNewsAction.value);
			}
        }
    }
    else
	{
        alert('Please enter a valid Name & E-mail address.');
	}
	
	if (bValidEmail)
	{
		url = "NewsletterSubscriptionProcess.asp?usrname="+dName.value+"&email="+oEmail.value+"&hNewsAction="+oForm.hNewsAction.value;
		// alert(url);
		ajaxFunction(url);
	}

    return bValidEmail;
}

//-----------
function ajaxFunction(url)
{
var xmlHttp = null;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    try
      {
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    catch (e)
      {
      alert("Your browser does not support AJAX!");
      return false;
      }
    }
  }
  xmlHttp.onreadystatechange=function()
    {
    if(xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
      {
		  document.getElementById("msgBoard").innerHTML=xmlHttp.responseText;
      }
    }
  xmlHttp.open("GET",url,true);
  xmlHttp.send(null);
}

