var errMsg='';
var email;
function emailValdn() {
	 email = jQuery.trim($("#email").val());
	if (email.length == 0){
		errMsg = "Please enter your email. \n";
	}
	else if (echeck(email)==false){
		errMsg = "Please enter valid email. \n";
	}
}

function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		    
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		    
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		     
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		     
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		     
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		     
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		     
		    return false
		 }

 		 return true					
	}

function ValidateForm(){
	var emailID=document.frmSample.txtEmail
	
	if ((emailID.value==null)||(emailID.value=="")){
		alert("Please Enter your Email ID")
		emailID.focus()
		return false
	}
	if (echeck(emailID.value)==false){
		emailID.value=""
		emailID.focus()
		return false
	}
	return true
 }
function validate_subscription() {
	errMsg = '';
	emailValdn();
	if (errMsg.length)
		alert(errMsg);
	else {	
		document.forms['formSubscription'].submit();
		/*var serializedData = $("#formSubscription").serialize();
		
		$.ajax({
   			type: "POST",
   			url: "form_process_subscription.php",
   			data: serializedData,
   			success: function(response) {
				alert('Got Ajax Response');
				if(response == '0')
					alert('Could not subscribe the news letter, please try again later.');
				else if(response == '1')
					alert('Subscription done for '+ email);
				else if(response == '2')
					alert('Subscribed the news letter but could not send the email.');
				else
     				alert (response);
				//window.location.href='thankyou_subscription.php';
				
   			}
 		});*/
	}
	return false;
}
function validate_subscription_index() {
	errMsg = '';
	emailValdn();
	if (errMsg.length){
		$("#subscriptionAlertBox").html('<p>'+errMsg+'</p><p><a href="#" style="color:#FF6600;">Close</a></p>');
		$("#subscriptionAlertBox").show();
		$("#subscriptionAlertBox a").bind('click',clickCloseLink);
	}
	else {	
		//document.forms['formSubscription'].submit();
		var serializedData = $("#formSubscription").serialize();
		var successfulSubscription = '<p>Thank you, for subscribing the newsletter. A notification mail has been sent to you on this email : '+email+'</p>';
		var successfulSubscriptWithoutEmail = '<p>Thank you, for subscribing the newsletter. Notification email was not sent.</p>';
		var unsuccessfulSubscription = '<p>Sorry, could not subscribe to the news letter, please try again later.</p>';
		$.ajax({
   			type: "POST",
   			url: "form_process_subscription_index.php",
   			data: serializedData,
   			success: function(response) {
				if(response == '0'){
					$("#subscriptionAlertBox").html(unsuccessfulSubscription+'<p><a href="#" style="color:#FF6600;">Close</a></p>');
					$("#subscriptionAlertBox").show();
					$("#subscriptionAlertBox a").bind('click',clickCloseLink);
				}
				else if(response == '1'){
					$("#subscriptionAlertBox").html(successfulSubscription+'<p><a href="#" style="color:#FF6600;">Close</a></p>');
					$("#subscriptionAlertBox").show();
					$("#subscriptionAlertBox a").bind('click',clickCloseLink);
				}
				else if(response == '2'){
					$("#subscriptionAlertBox").html(successfulSubscriptWithoutEmail+'<p><a href="#" style="color:#FF6600;">Close</a></p>');
					$("#subscriptionAlertBox").show();
					$("#subscriptionAlertBox a").bind('click',clickCloseLink);
				}
				else
     				{}
				//window.location.href='thankyou_subscription.php';
				
   			}
 		});
	}
	return false;
}
$("#subscribeImg").click(function() {
	//alert('CLICKED ME');
	return validate_subscription_index();
});
$("#Image15").click(function() {
	//alert('CLICKED ME');
	return validate_subscription();
});
/*$("#subscribe_image").click(function() {
	//alert('CLICKED ME');
	return validate();
});*/
$("form#formSubscription a:visible").click(function() {
	//alert('Testing ');
	return validate_subscription();
	//return call_another_function();
});
$("#subscriptionAlertBox a").bind('click',clickCloseLink);
function clickCloseLink(){
	$("#subscriptionAlertBox").hide();
}
