/*
Strip whitespace from the beginning and end of a string
Input : a string
*/
function trim(str)
{
	return str.replace(/^\s+|\s+$/g,'');
}

/*
Make sure that textBox only contain number
*/
function checkNumber(textBox)
{
	if(textBox.value=="")
	{
		textBox.value = 1;
	}
	while (textBox.value.length > 0 && isNaN(textBox.value) && textBox.value!=0) {
		textBox.value = textBox.value.substring(0, textBox.value.length - 1)
	}
	
	if(textBox.value==0 && textBox.value.length==1){
		textBox.value = 1;
	} else {
		textBox.value = trim(textBox.value);
	}
/*	if (textBox.value.length == 0) {
		textBox.value = 0;		
	} else {
		textBox.value = parseInt(textBox.value);
	}*/
}

/*
	Check if a form element is empty.
	If it is display an alert box and focus
	on the element
*/
function isEmpty(formElement, message) {
	formElement.value = trim(formElement.value);
	
	_isEmpty = false;
	if (formElement.value == '') {
		_isEmpty = true;
		alert(message);
		formElement.focus();
	}
	
	return _isEmpty;
}

/*
	Set one value in combo box as the selected value
*/
function setSelect(listElement, listValue)
{
	for (i=0; i < listElement.options.length; i++) {
		if (listElement.options[i].value == listValue)	{
			listElement.selectedIndex = i;
		}
	}	
}


function NewsLetterValidation()
{
	//var FirstName = $("#FirstName").val();
	var EmailAddress = $("#EmailAddress").val();
	var SuccessURL = $("#SuccessURL").val();
	var FailureURL = $("#FailureURL").val();
	var SiteURL = $("#SiteURL").val();
	
	/*if(FirstName=="")
	{
		alert("Please enter First Name.");
		$("#FirstName").focus();
		return false;
	}
	if(FirstName=="First Name")
	{
		alert("Please enter First Name.");
		$("#FirstName").focus();
		return false;
	}*/
	if(EmailAddress=="")
	{
		alert("Please enter Email Id.");
		$("#EmailAddress").focus();
		return false;
	}
	if(EmailAddress=="Mail ID")
	{
		alert("Please enter Email Id.");
		$("#EmailAddress").focus();
		return false;
	}
	var checkEmail = "@.";
	var checkStr = EmailAddress;
	var EmailValid = false;
	var EmailAt = false;
	var EmailPeriod = false;
	for (i = 0;  i < checkStr.length;  i++)
	{
		ch = checkStr.charAt(i);
		for (j = 0;  j < checkEmail.length;  j++)
		{
			if (ch == checkEmail.charAt(j) && ch == "@")
			EmailAt = true;
			if (ch == checkEmail.charAt(j) && ch == ".")
			EmailPeriod = true;
			if (EmailAt && EmailPeriod)
			break;
			if (j == checkEmail.length)
			break;
		}
		
		if (EmailAt && EmailPeriod)
		{
			EmailValid = true;
			break;
		}
	}
	if (!EmailValid)
	{
		alert("Please enter valid Email Id.");
		$("#EmailAddress").focus();
		$("#EmailAddress").val('');
		return false;
	}	
	else
	{
		//alert(SiteURL);
		$("#ajax-loader").show("slow");
		//$.post(SiteURL+"CCNewsletter/signup/index.php", { FirstName: FirstName, EmailAddress: EmailAddress, SuccessURL: SuccessURL, FailureURL: FailureURL }, 
		$.post(SiteURL+"CCNewsletter/signup/index.php", { EmailAddress: EmailAddress, SuccessURL: SuccessURL, FailureURL: FailureURL }, 
			function(data){
				//alert("DATA LOADED: "+data);
				$("#DisplayNewsletterResult").html(data);
				$("#ajax-loader").hide("slow");
				//$("#FirstName").val('First Name');
				$("#EmailAddress").val('email address');
			}
		);
		return false;
	}
	
	
	return true;
}


function FooterNewsLetterValidation()
{
	//var FirstName = $("#FirstName").val();
	var EmailAddress = $("#EmailAddressF").val();
	var SuccessURL = $("#SuccessURL").val();
	var FailureURL = $("#FailureURL").val();
	var SiteURL = $("#SiteURL").val();
	
	/*if(FirstName=="")
	{
		alert("Please enter First Name.");
		$("#FirstName").focus();
		return false;
	}
	if(FirstName=="First Name")
	{
		alert("Please enter First Name.");
		$("#FirstName").focus();
		return false;
	}*/
	if(EmailAddress=="")
	{
		alert("Please enter Email Id.");
		$("#EmailAddressF").focus();
		return false;
	}
	if(EmailAddress=="Mail ID")
	{
		alert("Please enter Email Id.");
		$("#EmailAddressF").focus();
		return false;
	}
	var checkEmail = "@.";
	var checkStr = EmailAddress;
	var EmailValid = false;
	var EmailAt = false;
	var EmailPeriod = false;
	for (i = 0;  i < checkStr.length;  i++)
	{
		ch = checkStr.charAt(i);
		for (j = 0;  j < checkEmail.length;  j++)
		{
			if (ch == checkEmail.charAt(j) && ch == "@")
			EmailAt = true;
			if (ch == checkEmail.charAt(j) && ch == ".")
			EmailPeriod = true;
			if (EmailAt && EmailPeriod)
			break;
			if (j == checkEmail.length)
			break;
		}
		
		if (EmailAt && EmailPeriod)
		{
			EmailValid = true;
			break;
		}
	}
	if (!EmailValid)
	{
		alert("Please enter valid Email Id.");
		$("#EmailAddressF").focus();
		$("#EmailAddressF").val('');
		return false;
	}	
	else
	{
		//alert(SiteURL);
		$("#ajax-loaderF").show("slow");
		//$.post(SiteURL+"CCNewsletter/signup/index.php", { FirstName: FirstName, EmailAddress: EmailAddress, SuccessURL: SuccessURL, FailureURL: FailureURL }, 
		tb_show('Newsletter', SiteURL+'CCNewsletter/signup/index.php?EmailAddress='+EmailAddress+'&SuccessURL='+SuccessURL+'&FailureURL='+FailureURL+'&width=420&height=100', '');
		$("#ajax-loaderF").hide("slow");
		//$("#FirstName").val('First Name');
		$("#EmailAddressF").val('email address');
		/*$.post(SiteURL+"CCNewsletter/signup/index.php", { EmailAddress: EmailAddress, SuccessURL: SuccessURL, FailureURL: FailureURL }, 
			function(data){
				//alert("DATA LOADED: "+data);
				//tb_show(data, SiteURL+'CCNewsletter/signup/index.php?EmailAddress='+EmailAddress+'&SuccessURL='+SuccessURL+'&FailureURL='+FailureURL+'&width=420&height=100', '');
				$("#ajax-loader").hide("slow");
				//$("#FirstName").val('First Name');
				$("#EmailAddress").val('email address');
			}
		);*/
		return false;
	}
	
	
	return true;
}
