//------------------------------------------------------------------------------------
//Validation for mandatory fields
//------------------------------------------------------------------------------------
function china() {
	alert("Chinthana lanakthilaka");
	return false;
}

function CheckMandatory(oForm, MandatoryString) {
	
	var Completed = true;

	var temp = oForm.elements.length;
	alert(temp);
	alert(oForm);return false;
	//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 setValue(eID) {
	//var eVal = document.getElementById(eID).value;
	//alert(eVal);
	

	if (trim(this.value) == 'Name' ){
		document.getElementById(this.id).value = "";
	}
	if (trim(this.value) == 'E-mail Address') {
		document.getElementById(this.id).value = "";
	}
	
}

function ResetValue(){

	if (trim(this.value) == "" && this.id == "txt_name") {
		document.getElementById(this.id).value = 'Name';
	}

	if (trim(this.value) == "" && this.id == "txt_email") {
		document.getElementById(this.id).value = 'E-mail Address';
	}


}
function ResetValueFanClub() {

    if (trim(this.value) == "" && this.id == "txt_name") {
		document.getElementById(this.id).value = 'Name';
	}

	if (trim(this.value) == "" && this.id == "txt_email") {
		document.getElementById(this.id).value = 'E-mail Address';
	}
}

function ResetPress() {

	if (trim(this.value) == "" && this.id == "press_name") {
		document.getElementById(this.id).value = 'Name';
	}

	if (trim(this.value) == "" && this.id == "press_email") {
		document.getElementById(this.id).value = 'E-mail Address';
	}
}
//...............................................................................
function ResetValue1(){

	if (trim(this.value) == "" && this.id == "ctl00_plcMain_GetInTouch_BizFormControl1_Bizform1_ctl00_getintouchName"  ){
		document.getElementById(this.id).value = 'Name *';
	}

	if (trim(this.value) == "" && this.id == "ctl00_plcMain_GetInTouch_BizFormControl1_Bizform1_ctl00_getintouchEmail"  ){
		document.getElementById(this.id).value = 'E-mail *';
	}

	if (trim(this.value) == "" && this.id == "ctl00_plcMain_GetInTouch_BizFormControl1_Bizform1_ctl00_getintouchTelephone" ){
		document.getElementById(this.id).value = 'Telephone *';
	}

	if (trim(this.value) == "" && this.id == "ctl00_plcMain_GetInTouch_BizFormControl1_Bizform1_ctl00_getintouchEnquiry"  ){
		document.getElementById(this.id).value = 'Enquiry *';
	}
	if (trim(this.value) == "" ){
		document.getElementById(this.id).value = 'Company';
	}
	if (trim(this.value) == "" ){
		document.getElementById(this.id).value = 'Position';
	}
	if (trim(this.value) == "" ){
		document.getElementById(this.id).value = 'Address';
	}
	if (trim(this.value) == "" ){
		document.getElementById(this.id).value = 'Postcode';
	}
	if (trim(this.value) == "" ){
		document.getElementById(this.id).value = 'Fax';
	}
	if (trim(this.value) == 'Website address' ){
		document.getElementById(this.id).value = 'Website address';
	}
}

//...............................................................................

//...............................................................................
function ResetValue2(){



	if (trim(this.value) == "" && this.id == "ctl00_plcMain_CMSEditableRegion2_BizFormControl1_Bizform1_ctl00_requestaquoteName"  ){
		document.getElementById(this.id).value = 'Name *';
	}

	if (trim(this.value) == "" && this.id == "ctl00_plcMain_CMSEditableRegion2_BizFormControl1_Bizform1_ctl00_requestaquoteEmail"  ){
		document.getElementById(this.id).value = 'E-mail *';
	}

	if (trim(this.value) == "" && this.id == "ctl00_plcMain_CMSEditableRegion2_BizFormControl1_Bizform1_ctl00_requestaquoteTelephone" ){
		document.getElementById(this.id).value = 'Telephone *';
	}

	if (trim(this.value) == "" && this.id == "ctl00_plcMain_CMSEditableRegion2_BizFormControl1_Bizform1_ctl00_Enquiry"  ){
		document.getElementById(this.id).value = 'Enquiry *';
	}
	if (trim(this.value) == ""  && this.id == "ctl00_plcMain_CMSEditableRegion2_BizFormControl1_Bizform1_ctl00_requestaquoteCompany" ){ 
		document.getElementById(this.id).value = 'Company';
	}
	if (trim(this.value) == "" && this.id == "ctl00_plcMain_CMSEditableRegion2_BizFormControl1_Bizform1_ctl00_requestaquotePosition" ){
		document.getElementById(this.id).value = 'Position';
	}
	if (trim(this.value) == ""  && this.id == "ctl00_plcMain_CMSEditableRegion2_BizFormControl1_Bizform1_ctl00_requestaquoteAddress"){
		document.getElementById(this.id).value = 'Address';
	}
	if (trim(this.value) == "" && this.id == "ctl00_plcMain_CMSEditableRegion2_BizFormControl1_Bizform1_ctl00_requestaquotePostalcode"){
		document.getElementById(this.id).value = 'Postcode';
	}
	if (trim(this.value) == "" && this.id == "ctl00_plcMain_CMSEditableRegion2_BizFormControl1_Bizform1_ctl00_requestaquoteFax"){
		document.getElementById(this.id).value = 'Fax';
	}
	if (trim(this.value) == "" && this.id == "ctl00_plcMain_CMSEditableRegion2_BizFormControl1_Bizform1_ctl00_requestaquoteWebsiteAddress" ){
		document.getElementById(this.id).value = 'Website address';
	}
}

//...............................................................................




function checkValue(eID,strVal) {
	var eVal = document.getElementById(eID).value;
	if (trim(eVal) == '' ){
		document.getElementById(eID).value = strVal;
	}
}


// -----------
function ValidatePressMedia()
{


	//var oForm = document.getElementById("ctl00_cplBottom_CMSEditableRegion2_BizFormControl1_Bizform1");
	var fEmail = document.getElementById("ctl00_cplBottom_CMSEditableRegion2_BizFormControl1_Bizform1_ctl00_ContactEmail_txtEmailInput");
	var fName = document.getElementById("ctl00_cplBottom_CMSEditableRegion2_BizFormControl1_Bizform1_ctl00_ContactName");
    
    var MandatoryString, bValidEmail;
	var url, htmlMsgBoardID;


	
	
    bValidEmail = false;
    


    if ((trim(fName.value) != 'Name') && (trim(fEmail.value) != 'E-mail Address'))
    {
            if (emailcheck(trim(fEmail.value)) == true) {
            	
				
                bValidEmail = true;
				
			}
       
    }
    else
	{
	
        alert('Please enter valid required details.');
		return false;
	}
	
    return bValidEmail;
   }
   //-------------------
   function ValidateFunclub() {


   //	var oForm = document.getElementById("ctl00_cplBottom_CMSEditableRegion2_BizFormControl1_Bizform1");
   	var fEmail = document.getElementById("ctl00_cplBottom_CMSEditableRegion1_BizFormControl1_Bizform1_ctl00_ContactEmail_txtEmailInput");
   	var fName = document.getElementById("ctl00_cplBottom_CMSEditableRegion1_BizFormControl1_Bizform1_ctl00_ContactName");

   	bValidEmail = false;



   	if ((trim(fName.value) != 'Name') && (trim(fEmail.value) != 'E-mail Address')) {
   		if (emailcheck(trim(fEmail.value)) == true) {


   			bValidEmail = true;

   		}

   	}
   	else {

   		alert('Please enter valid required details.');
   		return false;
   	}

   	return bValidEmail;
   }

function ValidateGetInTouchSubscription()
{
	var oForm  = document.getElementById("GetInTouch");
	var fEmail        = document.getElementById("fEmail");
    var fName         = document.getElementById("fName");
    var fTelephone    = document.getElementById("fTelephone");
    var fEnquiry      = document.getElementById("fEnquiry");
    //var fTitle        = document.getElementById("hFrmTitle");
    var MandatoryString, bValidEmail;
	var url, htmlMsgBoardID;
    
    bValidEmail = false;
    MandatoryString = "fName~fEmail~fTelephone~fEnquiry";
    
    if ((trim(fName.value) != 'Name *') && (trim(fEmail.value) != 'E-mail *') && (trim(fName.value) != 'Telephone *') && (trim(fName.value) != 'Enquiry *'))
    {
        if (CheckMandatory(oForm, MandatoryString) == true)
        {
            if (emailcheck(fEmail.value) == true)
			{
				oForm.hFrmAction.value = "Subscribe";
                bValidEmail = true;
				// alert(oForm.hFrmAction.value);
			}
        }
    }
    else
	{
        alert('Please enter valid required details.');
	}
	
	if (bValidEmail)
	{
		url = "GetInTouchProcess.asp?fName="+fName.value+"&fEmail="+fEmail.value+"&fTelephone="+fTelephone.value+"&fEnquiry="+fEnquiry.value+"&hFrmAction="+oForm.hFrmAction.value;
		htmlMsgBoardID = "msgBoardGetInTouch";
		// alert(url);
		ajaxFunction(url, htmlMsgBoardID);
	}

    return bValidEmail;
}
//-----------------------------------



function ValidateRequestAQuoteSubscription()
{
	var oForm  = document.getElementById("RequestAQuote");
	var fEmail        = document.getElementById("f2Email");
    var fName         = document.getElementById("f2Name");
    var fTelephone    = document.getElementById("f2Telephone");
    var fEnquiry      = document.getElementById("f2Enquiry");
    var fCompany      = document.getElementById("f2Company");
    var fPosition     = document.getElementById("f2Position");
    var fAddress      = document.getElementById("f2Address");
    var fPostcode     = document.getElementById("f2Postcode");
    var fFax          = document.getElementById("f2Fax");
    var fWebsiteAddress = document.getElementById("f2WebsiteAddress");
    var fHearUs       = document.getElementById("f2HearUs");
    var fChkBox       = '';

    var MandatoryString, bValidEmail;
	var url, htmlMsgBoardID;
    
    bValidEmail = false;
    MandatoryString = "f2Name~f2Email~f2Telephone~f2Enquiry";
    
    if ((trim(fName.value) != 'Name *') && (trim(fEmail.value) != 'E-mail *') && (trim(fName.value) != 'Telephone *') && (trim(fName.value) != 'Enquiry *'))
    {
        if (CheckMandatory(oForm, MandatoryString) == true)
        {
            if (emailcheck(fEmail.value) == true)
			{
				oForm.h2FrmAction.value = "Subscribe";
                bValidEmail = true;
				// alert(oForm.hFrmAction.value);
			}
        }
    }
    else
	{
        alert('Please enter valid required details.');
	}
	
	if (bValidEmail)
	{
        // prepare selected requiremnts as a string
        if (oForm.f2ChkBox.length>1){
            for(var i=0; i < oForm.f2ChkBox.length; i++){
                if(oForm.f2ChkBox[i].checked){
                    fChkBox +=oForm.f2ChkBox[i].value + "/";
                }
            }
        } else {
            fChkBox = oForm.f2ChkBox.value;
        }
       // document.getElementById("f2ChkBox");
   
		url = "RequestAQuoteProcess.asp?fName="+fName.value+"&fEmail="+fEmail.value+"&fTelephone="+fTelephone.value+"&fEnquiry="+fEnquiry.value+"&hFrmAction="+oForm.h2FrmAction.value+
		        "&fCompany="+fCompany.value+"&fPosition="+fPosition.value+"&fAddress="+fAddress.value+"&fPostcode="+fPostcode.value+
		        "&fFax="+fFax.value+"&fWebsiteAddress="+fWebsiteAddress.value+"&fHearUs="+fHearUs.value+"&fChkBox="+fChkBox;

		htmlMsgBoardID = "msgBoardRequestAQuote";
		//alert(url);
		ajaxFunction(url, htmlMsgBoardID);
	}

    return bValidEmail;
}

//-----------
function ajaxFunction(url, htmlMsgBoardID)
{

var xmlHttp = null;
//var eVal = document.getElementById(htmlMsgBoardID);
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(htmlMsgBoardID).innerHTML=xmlHttp.responseText;
      }
    }
  xmlHttp.open("GET",url,true);
  xmlHttp.send(null);
}
