//ajax
//--------------------------------------------------------------------

var ReturnID;
function ajaxRequest(strURL,strReturnID,strRequests,strMethod){
        
        ReturnID = strReturnID;
        
        var myAjax = new Ajax.Request(
        strURL,{
                method: strMethod,
                parameters: strRequests,
                onSuccess: pageAjaxSuccess,
                onLoading: pageAjaxLoading,
                onFailure: pageAjaxError,
                onExecption: pageAjaxError
        });
        
}
//ajax eventhandlers
function pageAjaxSuccess(transport){
        //document.getElementById(ReturnID).innerHTML = "<p style='color:red'>success</p>";
        //post to entellium asp which redirects to either crm_success_page.php or crm_failure_page.php
        //document.FrmInput.action = 'http://216.35.64.69/esam/eSalesForceAdmin/Create_WebLead.asp?Action=1';                
        document.FrmInput.action = 'http://www.entelliumblue.net/esam/eSalesForceAdmin/Create_WebLead.asp?Action=1';        // Updated from IP address to domain name.
        document.FrmInput.method = 'post';
        document.FrmInput.submit();

        //alert((transport.responseText==true ? 'sent' : 'failed'));
}
function pageAjaxLoading(){
        //document.getElementById(ReturnID).innerHTML = "<p style='color:red'>loading</p>";
}
function pageAjaxError(){
        //document.getElementById(ReturnID).innerHTML = "<p style='color:red'>error</p>";
        //redirect to failure page
        window.location = "crm_failure_page.php"
}

//  check for valid numeric strings
function IsNumeric(strString){
	var strValidChars = "0123456789.";
	var strChar;
	var blnResult = true;

	if (strString.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 IsValidEmail(src) {
	var emailReg = "^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$";
	var regex = new RegExp(emailReg);
	return regex.test(src);
}

function TextAreaMaxLength( field, countfield, maxlimit ) {
//syntax: parameters should be passed as this.form.fieldname
//maxlimit is number
	if ( field.value.length > maxlimit )
	{
		field.value = field.value.substring( 0, maxlimit );
		alert( 'Textarea value can only be ('+ maxlimit + ') characters in length.' );
		return false;
	}
	else
	{
		//alert(countfield == null);
		if (countfield != '' && countfield != null){
			countfield.value = maxlimit - field.value.length;
		}
	}
}

function trim(s) {
	while (s.substring(0,1) == ' ') {
		s = s.substring(1,s.length);
	}
	while (s.substring(s.length-1,s.length) == ' ') {
		s = s.substring(0,s.length-1);
	}
	return s;
}

//-----------------------------
//added by Gareth Williams: append referrer string to name field
function appendDomToField(strField){
	//search referrer string for one of the recognised txtnation domains and append to Name
	var strDomain = document.domain;
	var arrMyPages = new Array('txtnation','m-chat','mpush','m-enable','m-bill','mfusion','mverify','yourmobilebusiness','adultmobilesolutions','m-fusion');
	var strMyAppend = '';
	for (var intIndex=0; intIndex<arrMyPages.length; intIndex++){
		var strMySite = arrMyPages[intIndex];
		if (strDomain.search(strMySite) != -1){//search returns -1 for false
			if (strDomain != ''){//trap empty referrer
				strMyAppend = arrMyPages[intIndex].toString();
				var intSiteIndex = parseInt(intIndex);
			}
		}
		if (strMyAppend == ''){//only process none if append hasn't been assigned a value already
			strMyAppend = 'none';
		}
	}
	if (strMyAppend == ''){//no match for referrer in array but referrer field is not empty
		strMyAppend = strDomain;
	}
	//append referrer to name field entry
	eval ('document.FrmInput.' + strField + '.value = document.FrmInput.' + strField + '.value + \'(\' ' + ' + \'' + strMyAppend + '\' + ' + '\')\'');	
	//return domain identified if required
	return strMyAppend;
}
//-----------------------
function FormSubmit(Action, ValidateFields){
	var bError = false;
	var message = '';
	message = 'The following are required fields:\n\n';
	if (ValidateFields != ''){
		var arrFields = ValidateFields.split(',');
		for (var i=0; i < arrFields.length; i++){
			var FieldItem = arrFields[i].split('*');
			//if any required fields are left blank
			if (eval('document.FrmInput.'+FieldItem[0]+'.value') == ''){
    			message = message +'   * ' + FieldItem[1]+ '\n';
    			bError = true;
			}

		}
	}
	
	if (bError == false){
		
		var strDomFrom = appendDomToField('_AccountName230787')//see function def above
		
	    //Added By Lai On 15/11/2006
	    //Disable the button after user submit form
	    document.getElementById('Button1').disabled=true;
	    //document.getElementById('Button2').disabled=true;

	    //build up post string (ternary to conditionally include time in string)
	    var strPostVars =
'company='+ document.FrmInput._AccountName230787.value +
       '&firstname='+ document.FrmInput._FirstName230752.value +
       '&surname='+ document.FrmInput._LastName230755.value +
       '&email='+ document.FrmInput._OfficeEmail230764.value + 
		    '&site='+strDomFrom+
		 	'&date='+ document.getElementById('Meeting_date294992_day').value + '/' 
		 		+ document.getElementById('Meeting_date294992_month').value + '/' 
		 		+ document.getElementById('Meeting_date294992_year').value
		 	+ (document.getElementById('Meeting_Time279351_hour') !== null ?
			 	'&time=' + document.getElementById('Meeting_Time279351_hour').value + ':' 
			 	+ document.getElementById('Meeting_Time279351_minute').value : 
			 	'' )
	 	;
	 	
	   	
	   	ajaxRequest('message_handler.php','',strPostVars,'POST');
	}else{
		alert(message);
	}	
}
//Gareth Williams - set default date for form
function setDefault(){
	var timeObj = new Date();
	
	//date
	document.getElementById('Meeting_date294992_day').value = timeObj.getDate();
	document.getElementById('Meeting_date294992_month').value = timeObj.getMonth()+1;//remappped for entellium form
	document.getElementById('Meeting_date294992_year').value = timeObj.getFullYear();

	//time
	if(document.getElementById('Meeting_Time279351_hour')!==null){//if time inputs are on the form
		document.getElementById('Meeting_Time279351_hour').value = timeObj.getHours()<10?('0'+timeObj.getHours()):timeObj.getHours();//append 0 for 24 hour formatting
		document.getElementById('Meeting_Time279351_minute').value = timeObj.getMinutes()<10?('0'+timeObj.getMinutes()):timeObj.getMinutes();//append 0 for 24 hour formatting
		/*document.getElementById('Meeting_Time279351_second').value = timeObj.getSeconds();*/
	}
	return;	
}