// JavaScript Document
var spam="false";
var maxCharactersLimit = 160;
var count=0;
var splitTo=1;
var div=160;
function textCounter(field,countField,totalField,remField, cntSplit) {
	len = parseInt(field.value.length);
	if(len > 160){div = 153;}	
	if(len > 612){
		field.value = field.value.substring(0, maxCharactersLimit);	
		alert('You can not go beyond 4 pages!');
		return;
	}
	
	if (len > maxCharactersLimit){ 
		count=parseInt((len/div));splitTo=count;
		//splitTo=parseInt((len/div)-1);
		option = confirm('Page ' + splitTo + ' limit reached! Do you want to extend to the next page?');
		if(option == true){
				if(len%153>0){splitTo=count+1;}
				if(len <= 612){maxCharactersLimit = 612; splitTo=4;}
				if(len <= 459){maxCharactersLimit = 459;splitTo=3;}
				if(len <= 306){maxCharactersLimit = 306;splitTo=2;}
				if(len <= 160){maxCharactersLimit = 160;splitTo=1;}
		}else{
			field.value = field.value.substring(0, maxCharactersLimit);	
			len = parseInt(field.value.length);
		}
	}else{
		if(maxCharactersLimit == 612 && len <= 459){maxCharactersLimit = 459;splitTo=3;}
		if(maxCharactersLimit == 459 && len <= 306){maxCharactersLimit = 306;splitTo=2;}
		if(maxCharactersLimit == 306 && len <= 160){maxCharactersLimit = 160;splitTo=1;}
		if(maxCharactersLimit == 160 && len <= 160){maxCharactersLimit = 160;splitTo=1;}
		field.value = field.value.substring(0, maxCharactersLimit);	
		len = parseInt(field.value.length);
	}
	
	totalField.value = maxCharactersLimit ;
	cntSplit.value = parseInt(splitTo);
	countField.value = len + '/' + totalField.value;
	remField.value = 'Rem:' + (maxCharactersLimit - len);
}
var minDigitsInPhoneNumber = 13;
function validatePhoneNo(field){
	if (field.value.length > minDigitsInPhoneNumber) // if too long...trim it!
		field.value = field.value.substring(0, minDigitsInPhoneNumber);
	
	if (checkPhoneNumber(field.value)==false){
		alert('Invalid phone number Enter correct phone number');
		field.value = "";
		field.focus();
	}
}


function checkPhoneNumber(strPhone){
	return (isInteger(strPhone) && strPhone.length <= minDigitsInPhoneNumber);
}

function isInteger(s)
{   var i;
	for (i = 0; i < s.length; i++)
	{   
		// Check that current character is number.
		var c = s.charAt(i);
		if (((c < "0") || (c > "9"))) return false;
	}
	// All characters are numbers.
	return true;
}

/**************email validation*******************/
function validateEmail(field){
	if (!(!(field.value.match(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/)== null))) {  
		//alert('You must enter a valid email address');
		field.value = "";
		field.focus();
		return false;
	}
}

function checkMcabLogin()
{
	 if(isEmpty(document.mcabAdminLogin.username.value))
    {
        alert("Please enter your username.");
        document.mcabAdminLogin.username.focus();
    }
	else if(isEmpty(document.mcabAdminLogin.password.value))
    {
		alert("Please enter your password.");
        document.mcabAdminLogin.password.focus();
    }
	else{
		str = 'username=' + document.mcabAdminLogin.username.value + '&password=' + document.mcabAdminLogin.password.value;
		login(str);
	}
}

function checkElanProfile(){
	str = 'name=' + document.elanMemberProfile.name.value + '&companyName=' + document.elanMemberProfile.companyName.value + '&companyLocation=' + document.elanMemberProfile.companyLocation.value + '&companyAddress=' + document.elanMemberProfile.companyAddress.value + '&phone=' + document.elanMemberProfile.phone.value + '&email=' + document.elanMemberProfile.email.value + '&webURL=' + document.elanMemberProfile.webURL.value + '&others=' + document.elanMemberProfile.others.value;
	updateMember(str);
	//alert(str);
}

function checkElanMemberUpdate(){
	str = 'name=' + document.elanMemberProfile.name.value + '&companyName=' + document.elanMemberProfile.companyName.value + '&companyLocation=' + document.elanMemberProfile.companyLocation.value + '&companyAddress=' + document.elanMemberProfile.companyAddress.value + '&phone=' + document.elanMemberProfile.phone.value + '&email=' + document.elanMemberProfile.email.value + '&webURL=' + document.elanMemberProfile.webURL.value + '&others=' + document.elanMemberProfile.others.value + '&memberPostCode=' + document.elanMemberProfile.memberPostCode.value + '&paymentStatus=' + document.elanMemberProfile.paymentStatus.value + '&username=' + document.elanMemberProfile.username.value + '&password=' + document.elanMemberProfile.password.value + '&memberID=' + document.elanMemberProfile.memberID.value;
	updateMember(str);
	//alert(str);
}

function checkElanMemberDelete(){
	str = 'memberID=' + document.elanMemberProfile.memberID.value;
	deleteMember(str);
	//alert(str);
}

function checkInquiryForm(){
	if(isEmpty(document.inquiryForm.name.value))
    {
        alert("Please enter your name.");
        document.inquiryForm.name.focus();
    }
	else if(isEmpty(document.inquiryForm.email.value))
    {
        alert("Please enter your email.");
        document.inquiryForm.email.focus();
    }
	else if(isEmpty(document.inquiryForm.phone.value))
    {
        alert("Please enter your phone.");
        document.inquiryForm.phone.focus();
    }
	else if(isEmpty(document.inquiryForm.message.value))
    {
        alert("Please enter your message.");
        document.inquiryForm.message.focus();
    }
	else if(document.inquiryForm.phone.value.length < 11)
    {
        alert("Please check the mobile number!");
        document.inquiryForm.phone.focus();
    }
	else if(isEmpty(document.inquiryForm.recatcherEntry.value))
    {
		alert("Please enter the code above.");
        document.inquiryForm.recatcherEntry.focus();
    }
	else if(document.inquiryForm.recatcher.value != document.inquiryForm.recatcherEntry.value)
    {
		alert("Please you need to enter the code correctly!");
        document.inquiryForm.recatcherEntry.focus();
    }
	else{
		var interest="";
		if(document.getElementById("chk1").checked){interest+="Premium SMS, ";}
		if(document.getElementById("chk2").checked){interest+="Premium IVR, ";}
		if(document.getElementById("chk3").checked){interest+="Bulk SMS, ";}
		if(document.getElementById("chk4").checked){interest+="IVR, ";}
		if(document.getElementById("chk5").checked){interest+="Mobile Contents, ";}
		if(document.getElementById("chk6").checked){interest+="Wap Portal, ";}
		if(document.getElementById("chk7").checked){interest+="SMS, ";}
		if(document.getElementById("chk8").checked){interest+="Web Portal";}
		
		str = 'name=' + document.inquiryForm.name.value + '&option=' + document.inquiryForm.option.value + '&email=' + document.inquiryForm.email.value + '&phone=' + document.inquiryForm.phone.value + '&message=' + document.inquiryForm.message.value + '&interest=' + interest + '&companyName=' + document.inquiryForm.companyName.value;
	    sendInquiry(str);
	   //alert(str);
	}
}

function checkEquipmentForm()
{
	 if(isEmpty(document.equipmentForm.equipmentName.value))
    {
        alert("Please enter equipment name.");
        document.equipmentForm.equipmentName.focus();
    }
	else if(isEmpty(document.equipmentForm.description.value))
    {
		alert("Please provide short description.");
        document.equipmentForm.description.focus();
    }
	else{
		str = 'equipmentName=' + document.equipmentForm.equipmentName.value + '&description=' + document.equipmentForm.description.value;
		addEquipment(str);
		//alert(str);
	}
}

function checkEquipmentUpdate()
{
	 if(isEmpty(document.equipmentUpdate.equipmentName.value))
    {
        alert("Please enter equipment name.");
        document.equipmentUpdate.equipmentName.focus();
    }
	else if(isEmpty(document.equipmentUpdate.description.value))
    {
		alert("Please provide short description.");
        document.equipmentUpdate.description.focus();
    }
	else{
		str = 'equipmentName=' + document.equipmentUpdate.equipmentName.value + '&description=' + document.equipmentUpdate.description.value+ '&equipmentID=' + document.equipmentUpdate.equipmentID.value;
		updateEquipment(str);
		//alert(str);
	}
}

function checkElanSearchMessage(){
	str = 'equipmentName=' + document.elanSearchMessage.equipmentName.value + '&startDate=' + document.elanSearchMessage.startDate.value + '&endDate=' + document.elanSearchMessage.endDate.value + '&recipient=' + document.elanSearchMessage.recipient.value;
	//alert(str);
	getRequestDetails(str);
}

function checkElanMemberFeedbackMessage(){
	str = 'startDate=' + document.elanMemberFeedbackMessage.startDate.value + '&endDate=' + document.elanMemberFeedbackMessage.endDate.value + '&sender=' + document.elanMemberFeedbackMessage.sender.value;
	getResponseDetails(str);
	//alert(str);
}

function checkElanMember(){
	if(isEmpty(document.elanMemberSearch.memberID.value))
    {
        alert("Please select the member.");
        document.elanMemberSearch.memberID.focus();
    }
	else{
	str = 'memberID=' + document.elanMemberSearch.memberID.value;
	getThisMember(str);
	//alert(str);
	}
}

//utility functions

function isNumeric(strString)
   //  check for valid numeric strings	
   {
   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 isEmpty(field)
{
	field =trimEnter(field);
	return ((field == null) || (field.length == 0) || myTrim(field)=="");
}

function trimEnter(input)
{
	var tmp = "";

	for (var begin=0;begin <input.length;begin++)
	{
		var chrCode = input.charCodeAt(begin);
		if((chrCode!=13)&&(chrCode!=10))
		tmp+=input.charAt(begin);

	}
	return tmp;
}

function myTrim(str)
{
	var end = false;
	var ch;

	while(!end)
	{
		if (str.length == 0) break;
		ch = str.charAt(0);

		if (ch == ' ')
		{
			str = str.substring(1,str.length);
		}
		else
		{
			end = true;
		}
	}

	end = false;

	while(!end)
	{
		if (str.length == 0)
		{
			break;
		}

		ch = str.charAt(str.length-1);

		if (ch == ' ')
		{
			str = str.substring(0,str.length-1);
		}
		else
		{
			end = true;
		}
	}

	return str;
}
