﻿
/***************************/
//@Author: Adrian "yEnS" Mato Gondelle
//@website: www.yensdesign.com
//@email: yensamg@gmail.com
//@license: Feel free to use it, but keep this credits please!					
/***************************/

//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;
var popupStatus = 0;

//loading popup with jQuery magic!
function loadPopup2(){
	//loads popup only if it is disabled
	if(popupStatus==0){
		$("#backgroundPopup2").css({
			"opacity": "0.8"
		});
		$("#backgroundPopup2").fadeIn("slow");
		$("#popupContact2").fadeIn("slow");
		popupStatus = 1;
	}
}

//disabling popup with jQuery magic!
function disablePopup2(){
	//disables popup only if it is enabled
	if(popupStatus==1){
		$("#backgroundPopup2").fadeOut("slow");
		$("#popupContact2").fadeOut("slow");
		popupStatus = 0;
	}
}

//centering popup
function centerPopup2(){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#popupContact2").height();
	var popupWidth = $("#popupContact2").width();
	var fullHeight	=$(document).height();
	//centering
	$("#popupContact2").css({
		"position": "",
		"bottom": 2,
		"left": windowWidth/2-popupWidth/2				   
						   
		/*"position": "absolute",
		"top": windowHeight/2-popupHeight/2,
		"left": windowWidth/2-popupWidth/2*/
	});
	//only need force for IE6
	
	$("#backgroundPopup2").css({
		"height": windowHeight
	});
	
}


//CONTROLLING EVENTS IN jQuery
$(document).ready(function(){
	
	//CLOSING POPUP
	//Click the x event!
	$("#popupContactClose2").click(function(){
		disablePopup2(); // must call this to have SimpleModal 
	//	document.location.href	=	base_url_4_js+"/Index";
	clear_form2();
	});
	//Click out event!
	$("#backgroundPopup2").click(function(){
		disablePopup2();
		clear_form2();
	});
	
	$("#cancel2").click(function(){
		disablePopup2();
		clear_form2();
	});
	//Press Escape event!
	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			disablePopup2();
			clear_form2();
		}
	});

});


function submit_newsletter()
{
	var str	= "";//for taking the error msg
	var ctr	= "";//for taking the controllername for focus
	var emaill	=	$("#subemail").val();
	if($("#subname").val()==""){
		str	= 'Please give your name';
		ctr = "subname";
	}
	else if($("#subemail").val()==""){
		str	= 'Please give your email id';
		ctr = "subemail";
	}
	else if($("#subemail").val()!='' && !checkemail(emaill) ){	
	
		str	= 'Enter a valid email id';
		ctr = "subemail";
	}
	
	else if($("#captcha_input").val()==""){
	
		str	= 'Please type the code';
		ctr = "captcha_input";
	}
	
	
	else{
			var subname=$("#subname").val();
			var subemail=$("#subemail").val();
			//alert('before captcha check');
			captcha_for_newsletter(subname,subemail);	
	}


	
	if(str!="")
	$('#err_msg2').html(str+' | &nbsp;<a  href="javascript:void(0);" onClick="javascript:div_hide2(\''+ctr+'\');" >Hide</a>');
	
}//function



//function captcha validation(this an ajax function)
function captcha_for_newsletter(subname,subemail){
	
	captcha_input	= $('#captcha_input').val();
	captcha_id		= $('#captcha_id').val();
	
		$.ajax({
		   type: "POST",
		   url: base_site_url+'/Captcha/index',
		   data: "id=" + captcha_id + "& text=" + captcha_input,
		   success: function(msg){
			
				if(msg != "Passed CAPTCHA check"){
					
					$('#divcaptcha').html("");
					$('#divcaptcha').html(msg);	
					$('#msg_err').html("");
					if($('#captcha_input').val() != ""){
					
					$('#err_msg2').html('invalid code | &nbsp;<a  href="javascript:void(0);" onClick="javascript:div_hide2();" >Hide</a>');
					$('#captcha_input').val("");
					}
				}else{
					
					$.ajax({
					   type: "POST",
					   url: base_site_url+'/Newsletter/subscribe',
					   data: "subname=" + subname + "& subemail=" + subemail,
					   success: function(msg){
						  
						   if(msg=="success"){
						   $('#load_msg2').html('<script type="text/javascript" language="javascript">comments("You have successfully subscribed our news letters");</script>');
						   clear_form2();}
						   else{
							   $('#load_msg2').html('<script type="text/javascript" language="javascript">comments("Sorry..Some error has been occured. Try again!!");</script>');
						   }
						}
					   
					});
				}
			}
		 });
}//function


//function for hide message box
function div_hide2(ctr){
	
	$('#err_msg2').html("");
	$('#load_msg2').html("");
	
	document.getElementById(ctr).focus();
}

function clear_form2()
{
	$("#subname").val('');	
	$("#subemail").val('');
	$('#captcha_input').val("");
}

function checkemail(em)   //Email validation 
{
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(em))
	return true
	else
	return false
}	

