var JXFILE="jx/ajaxSignup.php";
var ERR_CITY = 'Select your city';
var tZC=flg='';
function changeCountry()	
{
		var vCnt = $("#countries").val();
		$("#imgZipCode").css("display","none");	
		if (vCnt == "United States" || vCnt == "Canada" ){
			$('#cptZip').html((vCnt == 'Canada')?"Postal Code":"Zip Code");
			showhide('#spnZip','#spnCity');			
			showImage('txtZipCode', 'imgZipCode');	
		}else if ((vCnt == "0" )){
			$('#spnZip, #spnCity').css("display","none");			
		} else{			
			_ajx (JXFILE, {type:"Country",pVal:vCnt,isC:$F('isC')}, _country, 'cntWait');
		}	
	}
function _country(r){
		$('#cntWait').empty();
		$('#spnCity').html(r.data);
		//$('#spnCity').show();
		showhide('#spnCity','#spnZip');
	}	
$(function(){
    $('#frdlbl, #bizlbl, #mchlbl, #txtDName, #countries, #vYearlbl').tipsy({gravity:$.fn.tipsy.autoWNS});
  
	pImgs(siteURL+"i/arrow-green.jpg",siteURL+"i/delete_icon.gif",siteURL+"i/load2.gif");
	
	showImage('txtFName', 'imgFName');
	showImage('txtLName', 'imgLName');
	showImage('txtEmail', 'imgEmail');
	showImage('txtDName', 'imgDName');
	showImage('countries', 'imgcountries');
	showImage('dobDate', 'imgDOB');
	showImage('dobMonth', 'imgDOB');
	showImage('dobYear', 'imgDOB');
	$('#spnCity').css("display","none");
	$('#spnZip').css("display","none");
	
	
	//$("#countries").blur(function(){$("#countries").change()});
	$("#countries").change(function(){
		changeCountry();
	});
	
	
	
	$('#vImageCodP').keypress(function (e){ if (e.which == 13) $("#imgSubmit").click();});
	$("#imgSubmit").click(function(){
		
		$('#erMsg').hide();	
		
		if(!$('#chkYrTrm').attr('checked'))
		{
			showMsgJs('#erMsg','Please verify that you are 18 years of age and agree to the Terms of Service.','aft','error');	
			return;
		}
		$SH('#imgSubmitLD','#imgSubmitBtn');
		
		var tZC = $('#txtZipCode').val();
		var vCnt = $("#countries").val();
		var vCty = (typeof (tZC)=='undefined' || tZC == "")?''+$F("cities"):tZC;
		var disYr = $R("vYear")==1?1:0;
		var vGndr = $R("rdGender");
		var fSrc = $R("frd")==1?1:0;
		var mSrc = $R("mch")==1?1:0;
		var bSrc = $R("biz")==1?1:0;
		var chkTrm = ($('#chkYrTrm').attr('checked')?1:0);
		var univID = (typeof ($('#univID'))=='undefined' || $F('univID') == "" ? '' : $F('univID') );
		$('#bsubmit').click();

		_ajx (JXFILE, {type:"Signup", email:$F("txtEmail"), fn:$F("txtFName"), ln:$F("txtLName"), dn:$F("txtDName"),gn:vGndr, ct:vCty, cn:vCnt, yob:$F('dobYear'), mob:$F('dobMonth'), dob:$F('dobDate'), isC:$F('isC'), vYr:disYr, ffSrc:fSrc, mmSrc:mSrc, bbSrc:bSrc,chbID:$F('chID'), chkTrm:chkTrm,vImageCodP:$F('vImageCodP'),univID:univID}, _posted);
		
	});
	$("#imgSubmitCmpg").click(function(){
		
		$('#erMsg').hide();	
		$SH('#imgSubmitLD','#imgSubmitBtn');
		
		var tZC = $('#txtZipCode').val();
		var vCnt = $("#countries").val();
		var vCty = (typeof (tZC)=='undefined' || tZC == "")?''+$F("cities"):tZC;
		var disYr = $R("vYear")==1?1:0;
		var vGndr = $R("rdGender");
		var fSrc = $R("frd")==1?1:0;
		var mSrc = $R("mch")==1?1:0;
		var bSrc = $R("biz")==1?1:0;
		var chkTrm = ($('#chkYrTrm').attr('checked')?1:0);
		$('#bsubmit').click();

		_ajx (JXFILE, {type:"SignupCmpg", email:$F("txtEmail"), fn:$F("txtFName"), ln:$F("txtLName"), dn:$F("txtDName"),gn:vGndr, ct:vCty, cn:vCnt, yob:$F('dobYear'), mob:$F('dobMonth'), dob:$F('dobDate'), isC:$F('isC'), vYr:disYr, ffSrc:fSrc, mmSrc:mSrc, bbSrc:bSrc,chbID:$F('chID'), chkTrm:chkTrm,vImageCodP:$F('vImageCodP')}, _postedCmpg);
		
	});
	
	//Added by naveed saleem
	$("#fbSubmit").click(function(){ fbSignup(); });
	//End added by naveed saleeem
	
	$('#rpwd').keypress(function (e){ if (e.which == 13) $("#bsubmit").click();});
	$('#bsubmit').click(function(){
		if ($F('xpwd') != '' && $F('npwd') != '' && $F('rpwd') != ''){
			if ($F('npwd') == $F('rpwd') ){
				_ajx (JXFILE, {type:'confirm', oldPwd: $F('xpwd'), newPwd: $F('npwd'), chbID:$F('chID')}, _pwdChanged, "ldGif");
				
			}else{				
				$('#erMsg p').html('Entered new and retype passwords not matched');
				$('#erMsg').fadeIn(500).fadeOut(8000);
			}
		}else{
			if ($F('xpwd') == '')
				$('#erMsg p').html('Enter emailed password');
			else if ($F('npwd') == '')
				$('#erMsg p').html('Enter new password');
			else
				$('#erMsg p').html('Retype new password');
			$('#erMsg').fadeIn(500).fadeOut(8000);
		}		
	 });	
	/*$('input,select').each(function (){
		if ($(this).attr("alt") != "" && (typeof $(this).attr("alt") != "undefined")){
			var elmID = $(this).attr("id");
			if ($(this).attr("type") == "checkbox"){
				$("#"+elmID+" ~ label").after("<div id='h_"+elmID+"' class='SgFrmTT' ><div class='arrTT'></div><div class='infTT'>"+$(this).attr("alt")+"</div");			
			}else{
				$(this).after("<div id='h_"+elmID+"' class='SgFrmTT' ><div class='arrTT'></div><div class='infTT'>"+$(this).attr("alt")+"</div");			
			}
			$(this).mouseover(function(){
				$('#h_'+elmID).attr("class","shInf");				
			});
			$(this).mouseout(function(){
				$('#h_'+elmID).attr("class","SgFrmTT");				
			});
			
			$(this).focus(function(){$(this).mouseover()});
			$(this).blur(function(){$(this).mouseout()});			
		}
	});*/
	//------------short signup code -----
	//$('#bPst').click(function(){		
//		$('#step1 input').each(function(){
//			if($(this).attr("value")=='')
//				flg += " "+$(this).attr('name');
//		});
//		if (flg != ''){
//			$('#erMsg p').html('Please fill all fields'+ flg);
//			$('#erMsg').fadeIn(500,function(){flg='';}).fadeOut(5000);
//			return false;
//		}else{			
//			//_ajx (JXFILE, {type:'shortSignup', fn:$F('tFN'), ln:$F('tLN'), eml:$F('tEml'), gndr:$R('rGndr')}, _bPst, "");
//			return true;
//		}
//	});
//	$('#tEml').blur(function(){
//		$('#bPst').attr("disabled","disabled");
//		_ajx (JXFILE, {type:'Email',pVal:$F('tEml')}, _tEml, "lodSignup");
//	});
	
});
//	function _tEml(r){
//		if (r.data != "1"){
//			$('#erMsg p').html('Email is invalid');
//			$('#erMsg').fadeIn(500).fadeOut(5000);
//		}else
//			$('#bPst').attr("disabled","");
//	}//----------short signup code
	//function _bPst(r){
	//	alert(r.data);
		//$('#step1').attr("class",'hide');
		//$('#mSuc').html('We sent a random password to "'+r.email+'". If you have not received it then please check your spam folder. <br /> <br />If you are having problems signing-up, please <a href="support.php">click here</a>. ');
		//$('#step2').attr("class",'logFrm');
	//}
	function _pwdChanged (r){			
		if (r.data.msgType == 'SUC'){
			window.location.href=r.data.nxp;			
		}else{			
			$('#erMsg p').html(r.data.msg);
			$('#erMsg').fadeIn(500).fadeOut(8000);
			//showMsg('#imgSubmit',r.data.msg,'bef');
		}
	}	
	function _posted(r){		
		if (r.data == 'RedirectToSignup'){
			window.location.href="signup.php?ac=cnfrm";
		}else if (r.data == 'RedirectToHome'){
			window.location.href="index-greeting.php";
			//window.location.href="fsi.php";
		}else if (r.data == 'RedirectToLogin'){
			window.location.href="login.php";
		}else{
			$SH('#imgSubmitBtn','#imgSubmitLD');
			$('#erMsg').show();	
			$('#erMsg p').html(r.data.msg);			
			//$('#erMsg').fadeIn(100).fadeOut(5000);
			showhide('#erMsg','none');
			refreshCaptcha("libs/img.php?size=4");
			//showMsg('#imgSubmit',r.data.msg,'bef');
		}
		
   }   
	function _postedCmpg(r){		
		if (r.data == 'RedirectToSignup'){
			window.location.href="cmpg-su.php?";
		}else{
			$SH('#imgSubmitBtn','#imgSubmitLD');
			$('#erMsg').show();	
			$('#erMsg p').html(r.data.msg);
			showhide('#erMsg','none');
		}
		
   }   
   function _email(r){ 
		if (r.data == 1){
			$("#imgEmail").attr("src",siteURL+"i/arrow-green.jpg").show();
			$("#sptxtEmail").html("");
			
		}else{
			$("#imgEmail").attr("src",siteURL+"i/delete_icon.gif").show();
			if(r.data == '2')
			{
				$("#sptxtEmail").html("(Already in Use)")
				$("#msgEmail").show();
			}
			else if(r.data == '3')
			{
				$("#sptxtEmail").html("(Already Registered)")
				$("#msgEmail2").show();
			}
			else
			{
				if($F("txtEmail")!='')
					$("#sptxtEmail").html("(Invalid Email Address)");
				else
					$("#sptxtEmail").html("");
			}
		}
	}
	function _dname(r){ 
		
		if (r.data == 1){
			$("#imgDName").attr("src",siteURL+"i/arrow-green.jpg").show();
			$("#sptxtDName").html("");
		}else{
			$("#imgDName").attr("src",siteURL+"i/delete_icon.gif").show();
			if( $F("txtDName") != '')
			{
				if(r.data == 2)
					$("#hntDName").show();
				else
					$("#sptxtDName").html("(Not Available)");
			}
		}
	}
	function _zipCode(r){
		if (r.data.msgType == 'SUC'){			
			$("#imgZipCode").attr("src",siteURL+"i/arrow-green.jpg").show();
		}else{			
			$("#imgZipCode").attr("src",siteURL+"i/delete_icon.gif").show();
		}
	}
	function showImage(fld, imgname){
		$("#"+fld).blur(function(){	
			$('#erMsg').hide();
			
			$("#"+imgname).attr("src",siteURL+"i/load2.gif").show();
			
			if ((fld == 'txtFName' || fld == 'txtLName') && $F(fld) != ""){
				_ajx(JXFILE, {type:"name", name:$F(fld)},function(r){
					if (r.data == 1){
						$("#"+imgname).attr("src",siteURL+"i/arrow-green.jpg").show();
						$("#sp"+fld).html("");
					}else{
						
						$("#"+imgname).attr("src",siteURL+"i/delete_icon.gif").show();
						if($F(fld)!='')
							$("#sp"+fld).html("(Alphabets Only)");
					}
				},"");
			}			
			else if (fld == 'txtZipCode'){
				_ajx(JXFILE, {type:"zipCode", zip:$F('txtZipCode'), cntry:$("#countries option:selected").val()},_zipCode,"");
			}			
			else if (fld == "txtEmail"){
				var pEmail = $F("txtEmail");
				$('#msgEmail').hide();
				$('#msgEmail2').hide();
				_ajx (JXFILE, {type:'Email',pVal:pEmail,isC:$F('isC')}, _email, "");
			}					
			else if (fld == "txtDName"){
				$("#spanText").html('');
				$("#hntDName").hide();
				var pDname = $F("txtDName");
				_ajx (JXFILE, {type:'DName',pVal:pDname}, _dname, "");
			}
			else if (imgname == "imgDOB"){
				if( $F("dobDate") == '' || $F("dobMonth") == '' || $F("dobYear") == '' )
					$("#imgDOB").attr("src",siteURL+"i/delete_icon.gif");
				else
					$("#imgDOB").attr("src",siteURL+"i/arrow-green.jpg");
				
				$("#imgDOB").css("display","");
			}
			else{
				if ($("#"+fld).val() != ""){	
					$("#" + imgname).attr("src",siteURL+"i/arrow-green.jpg");			
				}else{
					$("#" + imgname).attr("src",siteURL+"i/delete_icon.gif");			
				}
				$("#" + imgname).css("display","");
				$("#sp"+fld).html("");
			}
		});
	}

function showhide(toS, toH){
	if (toS != 'none')
		$(toS).fadeIn(500);
		
	if (toH != 'none')
		$(toH).fadeOut(200);
}

function refreshCaptcha(src){
	$('#vImageCodP').val("");
	$('#cptbox').empty();
	return $('#cptbox').each(function(){
		var rand = Math.random();
        var i = new Image();        
        i.src = src + '&t=' +rand;
		i.width="190";
		i.height="35";		
        $('#cptbox').append(i);
    });
}

//Added by naveed Saleem
function _fbposted(r){
	
	
	if (r.data == 'RedirectToHome'){
		window.location.href="index-greeting.php";
	}
	else
	{
		$SH('#imgSubmitBtn','#imgSubmitLD');
		$('#erMsg p').html(r.data.msg);	
		showhide('#erMsg','none');
	}
	
}   
//End Added By naveed Saleem   


//Added by naveed saleem
	function fbSignup()
	{
		$('#erMsg').hide();	
		
		if(!$('#chkYrTrm').attr('checked'))
		{
			showMsgJs('#erMsg','Please verify that you are 18 years of age and agree to the Terms of Service.','aft','error');
			return;
		}
		$SH('#imgSubmitLD','#imgSubmitBtn');
		
		tZC = $('#txtZipCode').val();
		vCnt = $("#countries").val();
		vCty = (typeof (tZC)=='undefined' || tZC == "")?''+$F("cities"):tZC;
		disYr = $R("vYear")==1?1:0;
		vGndr = $R("rdGender");
		var chkTrm = ($('#chkYrTrm').attr('checked')?1:0);
		
		_ajx (JXFILE, {type:"FBSignup", email:$F("txtEmail"), fn:$F("txtFName"), ln:$F("txtLName"), dn:$F("txtDName"), gn:vGndr, ct:vCty, cn:vCnt, yob:$F('dobYear'), mob:$F('dobMonth'), dob:$F('dobDate'), vYr:disYr, chkTrm:chkTrm, fb_uid:$F('fb_uid')}, _fbposted);	
		
	}
	//End added by naveed saleeem