// JavaScript Document
var do_login = function(){
	var bool = 1;
	$('show_error').update("");
	if($('username').value.blank()){
		bool = 0;		
	}
	if($('password').value.blank()){
		bool = 0;
	}	
	if(bool){
		var P = Form.serialize('login_frm');
		var url = "ajaxserver/user/login_response.php";
		new Ajax.Request(url, {
						 	method : 'get',
							parameters : P,
							onSuccess : function(transport){
								var res = transport.responseText;
								//alert(res);
								if(res == 1){
									$('show_error').update("Wrong login data");
									$('login_frm').reset();
									$('username').focus();
								}else if(res == 2){
									$('show_error').update("Status Inactive");
									$('login_frm').reset();
									$('username').focus();
								}
								else
								{
									var ret="";
									var qty="";									
									if($('return').value!="")
									{
										ret=$('return').value
										qty=$('query').value;
										//alert(ret+"?code="+qty);
										if(qty!="")
										{
											window.location.href = ret+"?code="+qty;
										}
										else if($('user_id').value!="")
										{
											qty=$('user_id').value;
											window.location.href = ret+"?user_id="+qty;	
										}
										else
										{
											window.location.href = ret;	
										}
									}
									else
									{
										
										if($('check_out').value!="")
										{
											window.location.href = 'process.php';
										}
										else
										{
											
											window.location.href = 'homepage.php';
										}
									}
								}
							},
							onFailure : function(){
								alert("Connection failure!");
							}
						 });
	}else{
		$('show_error').update("Both Username and Password are required");
		$('login_frm').reset();
		$('username').focus();
	}
}
var check_availibity = function(){
	if(!$('username').value.blank()){
		new Ajax.Request("ajaxserver/user/check_availibity.php", {
						 	method : 'get',
							parameters : { username : $('username').value },
							onSuccess : function(transport){
								var res = transport.responseText;
								//alert(res);
								if(res == 1){
									$('username_error').update("Username not available");
									$('username').focus();
								}else{
									$('username_error').update("<span style='color:#006600'>Username available</span>");
								}
							}
						 });
	}
	
}


var submit_reg_frm = function(){
	var bool = true;
	$('first_name').className ='textfield_signup_home';
	$('last_name').className ='textfield_signup_home';
	$('email_signup').className ='textfield_signup_home';
	$('day').className ='day_signup_home';
	$('month').className ='month_signup_home';
	$('year').className ='year_signup_home';
	
	if($('first_name').value.blank()){
		bool = false;
		$('first_name').className ='textfield_signup_home_error';
	}
	if($('last_name').value.blank()){
		bool = false;
		$('last_name').className ='textfield_signup_home_error';
	}
	if($('email_signup').value.blank()){
		bool = false;
		$('email_signup').className ='textfield_signup_home_error';
	}else{
		if(!isEmail($('email_signup').value)){
			bool = false;
			$('email_signup').className ='textfield_signup_home_error';
		}
	}
	if($('day').value.blank()){
		bool = false;
		$('day').className ='day_signup_home_error';
	}
	if($('month').value.blank()){
		bool = false;
		$('month').className ='month_signup_home_error';
	}
	if($('year').value.blank()){
		bool = false;
		$('year').className ='year_signup_home_error';
	}
	/*if(){
	}
	if($('dob').value.blank()){
		bool = false;
		$('dob').className ='textfield_signup_home__small_error';
	}*/
	return bool;
}

var fill_up = function(){
	if($('check_ship').checked == true){
		$('ship_address_first').value = $('bill_address_first').value;
		$('ship_address_sec').value = $('bill_address_sec').value;
		$('ship_city').value = $('bill_city').value;
		$('ship_state').value = $('bill_state').value;
		$('ship_zip').value = $('bill_zip').value;
		$('ship_phone').value = $('bill_phone').value;
	}else{
		$('ship_address_first').value = "";
		$('ship_address_sec').value = "";
		$('ship_city').value = "";
		$('ship_state').value = "";
		$('ship_zip').value = "";
		$('ship_phone').value = "";
	}
}

var do_register = function(){
	var bool = true;
	//get captcha data
	var ch = Recaptcha.get_challenge();
	var r = Recaptcha.get_response();
	clearErrorMsgs("error");
	var errorStr = "";
	if($('first_name').value.blank()){
		bool = false;
		$('first_name_error').update('Required');
		errorStr += 'first_name|';
	}
	if($('last_name').value.blank()){
		bool = false;
		$('last_name_error').update('Required');
		errorStr += 'last_name|';
	}
	if($('nickname').value.blank()){
		bool = false;
		$('nickname_error').update('Required');
		errorStr += 'nickname|';
	}
	if($('contact_email').value.blank()){
		bool = false;
		$('contact_email_error').update('Required');
		errorStr += 'contact_email|';
	}else{
		if(!isEmail($('contact_email').value)){
			bool = false;
			$('contact_email_error').update('Bad format');
			errorStr += 'contact_email|';
		}
	}
	if($('re_bill_email').value.blank()){
		bool = false;
		$('re_bill_email_error').update('Required');
		errorStr += 're_bill_email|';
	}else{
		if($('contact_email').value != $('re_bill_email').value){
			bool = false;
			$('re_bill_email_error').update('Email mismatch');
			errorStr += 're_bill_email|';
		}
	}
	if($('day').value.blank() || $('month').value.blank() || $('year').value.blank()){
		bool = false;
		$('dob_error').update('All three required');
		errorStr += 'day|';
	}
	
	
	if($('bill_address_first').value.blank() && $('bill_address_sec').value.blank()){
		bool = false;
		$('bill_address_first_error').update('Required');
		errorStr += 'bill_address_first|';
	}else{
		if(tagvalidate($('bill_address_first').value) || tagvalidate($('bill_address_sec').value)){
			bool = false;
			$('bill_address_first_error').update('HTML not allowed');
			errorStr += 'bill_address_first|';
		}
	}
	if($('bill_city').value.blank()){
		bool = false;
		$('bill_city_error').update('Required');
		errorStr += 'bill_city|';
	}
	if($('bill_state').value.blank()){
		bool = false;
		$('bill_state_error').update('Required');
		errorStr += 'bill_state|';
	}
	if($('bill_zip').value.blank()){
		bool = false;
		$('bill_zip_error').update('Required');
		errorStr += 'bill_zip|';
	}
	if($('bill_phone').value.blank()){
		bool = false;
		$('bill_phone_err').update('Required');
		errorStr += 'bill_phone|';
	}
	
	
	/*if($('card_merchant').value.blank()){
		bool = false;
		$('card_merchant_error').update('Required');
		errorStr += 'card_merchant|';
	}
	if($('card_number').value.blank()){
		bool = false;
		$('card_number_error').update('Required');
		errorStr += 'card_number|';
	}else{
		if($('card_merchant').value == "Visa"){
			//$("card_number").maxlength = 16;
			if($('card_number').value.length != 16){
				bool = false;
				$('card_number_error').update('Visa requires 16 digit Card number');
				errorStr += 'card_number|';
			}
		}
		if($('card_merchant').value == "American Express"){
			if($('card_number').value.length != 15){
				bool = false;
				$('card_number_error').update('American Express requires 15 digit Card number');
				errorStr += 'card_number|';
			}
		}
		if($('card_merchant').value == "Discover"){
			if($('card_number').value.length != 16){
				bool = false;
				$('card_number_error').update('Discover requires 16 digit Card number');
				errorStr += 'card_number|';
			}
		}
	}
	if($('exp_mm').value.blank() || $('exp_yy').value.blank()){
		bool = false;
		$('card_expiry_err').update('Required');
		errorStr += 'exp_mm|';
	}else{
		if(isNaN($('exp_mm').value) || isNaN($('exp_yy').value)){
			bool = false;
			$('card_expiry_err').update('Numeric value required');
			errorStr += 'exp_mm|';
		}
		if(($('exp_mm').value == "00") || ($('exp_yy').value == "00")){
			bool = false;
			$('card_expiry_err').update('Invalid input');
			errorStr += 'exp_mm|';
		}
	}
	if($('card_csc').value.blank()){
		bool = false;
		$('card_csc_error').update('Required');
		errorStr += 'card_csc|';
	}*/
	
	
	//shipment validation
	if($('ship_address_first').value.blank() && $('ship_address_sec').value.blank()){
		bool = false;
		$('ship_address_first_error').update('Required');
		errorStr += 'ship_address_first|';
	}else{
		if(tagvalidate($('ship_address_first').value) || tagvalidate($('ship_address_sec').value)){
			bool = false;
			$('ship_address_first_error').update('HTML not allowed');
			errorStr += 'ship_address_first|';
		}
	}
	if($('ship_city').value.blank()){
		bool = false;
		$('ship_city_error').update('Required');
		errorStr += 'ship_city|';
	}
	if($('ship_state').value.blank()){
		bool = false;
		$('ship_state_error').update('Required');
		errorStr += 'ship_state|';
	}
	if($('ship_zip').value.blank()){
		bool = false;
		$('ship_zip_error').update('Required');
		errorStr += 'ship_zip|';
	}
	if($('ship_phone').value.blank()){
		bool = false;
		$('ship_phone_err').update('Required');
		errorStr += 'ship_phone|';
	}
	
	//login validation
	if($('username').value.blank()){
		bool = false;
		$('username_error').update('Required');
		errorStr += 'username|';
	}
	if($('password').value.blank()){
		bool = false;
		$('password_error').update('Required');
		errorStr += 'password|';
	}else{
		if($('password').value.length < 6){
			bool = false;
			$('password_error').update('Minimum six characters required');
			errorStr += 'password|';
		}
	}
	if($('con_password').value.blank()){
		bool = false;
		$('con_password_error').update('Required');
		errorStr += 'con_password|';
	}else{
		if($('password').value != $('con_password').value){
			bool = false;
			$('con_password_error').update('Password mismatch');
			errorStr += 'con_password|';
		}
	}
	if($('secret_question').value.blank()){
		bool = false;
		$('secret_question_error').update('Required');
		errorStr += 'secret_question|';
	}
	if($('secret_answer').value.blank()){
		bool = false;
		$('secret_answer_error').update('Required');
		errorStr += 'secret_answer|';
	}
	if(r.blank()){
		bool = false;
		$('security_code_error').update("Required");
		//errorStr += 'security_code_error';
	}
	if($('agree_flag').checked == false){
		bool = false;
		$('agree_flag_error').update('Needs to agree');
		errorStr += 'agree_flag|';
	}
	
	if(bool){
		$('signbut').hide();
		$('signload').show();
		var P = Form.serialize("signup_frm");
		
		P += "&ch="+ch+"&r="+r;
		var ajax_url = base64_decode($('response_page').value);
		new Ajax.Request(ajax_url, {
						 	method : 'get',
							parameters : P,
							onSuccess : function(transport){
								$('signbut').show();
								$('signload').hide();
								var res = transport.responseText;
								//alert(res);
								var resArray=res.split('#');
								
								var resCheck=resArray[0];	
								
								//resCheck=resCheck.replace('Could not execute: /var/qmail/bin/sendmail','');
								
								if(parseInt(resCheck) == 0)
								{
									var u_id=resArray[1];
									//alert(u_id);
								}
								if(parseInt(resCheck) == 0){
									
									/*window.location.href = 'homepage.php';*/
									$("signup_frm").reset();
									scroll(0,0);
									if($("check_out").value=="")
									{
										
										$('success_response').update('Thank you for signing up. The activation code is sent to your contact email address. Please check and confirm your registration.');
										setTimeout("window.location.href='login.php'", 10000);
									}
									else
									{
										
										$('success_response').update('Please continue with the check out proccess....');
										setTimeout("window.location.href='process.php?u_id="+u_id+"'", 5000);
									}
								}else{
									Recaptcha.reload();
									/*$('frm_error').update("Signup not possible, please check the erros above..");*/
									if(resCheck == 1){
										$('security_code_error').update('Incorrect Security Code');
									}
									if(resCheck == 2){
										$('username_error').update('Username not available');
										$('username').focus();								
										
									}
									if(resCheck == 3){
										$('contact_email_error').update('Contact email exists');
										$('contact_email').focus();
										
									}
									/*if(resCheck == 5){
										create_modal('ajaxserver/user/window_signup_message.php?res='+res,'350','160',true);
										setTimeout("create_modal('','','',false); $('card_number_error').update('Card number should be unique'); $('card_number').focus();", 3000);
										
										
									}
									if(resCheck == 6){
										create_modal('ajaxserver/user/window_signup_message.php?res='+res,'350','160',true);
										setTimeout("create_modal('','','',false); $('bill_validation_error').update('The City,State and Zip combination you have entered for Billing is incorrect'); $('bill_city').focus();", 3000);
										
										
									}
									if(resCheck == 7){
										create_modal('ajaxserver/user/window_signup_message.php?res='+res,'350','160',true);
										setTimeout("create_modal('','','',false); $('ship_validation_error').update('The City,State and Zip combination you have entered for Shipping is incorrect'); $('ship_city').focus();", 3000);
										
										
									}*/
								}
									
							}
						 });
	}else{
		if(errorStr != ""){
			var errorFArr = errorStr.split("|");
			$(errorFArr[0]).focus();
		}
	}
	
}


var fp_check_email = function(){
	var bool = true;
	$('fp_email_error').update("");
	if($('fp_email').value.blank()){
		$('fp_email_error').update("Required");
	}else{
		if(!isEmail($('fp_email').value)){
			bool = false;
			$('fp_email_error').update('Bad format');
		}
	}
	var fpe = $('fp_email').value;
	if(bool){
		var fp_ce_url = "ajaxserver/user/fp_check_email.php";
		$('fp_email_fire_rotater').show();
		$('fp_email_fire_button').hide();
		new Ajax.Request(fp_ce_url, {
						 	method : 'get',
							parameters : { e : fpe  },
							onSuccess : function(transport){
								var res = transport.responseText;
								var resSP = res.split("|");
								$('fp_email_fire_rotater').hide();
								$('fp_email_fire_button').show();
								if(resSP[0] == 0){									
									$('fp_email_error').update('Wrong email');
								}else{									
									
									$('fp_emailW').hide();
									$('fp_email_sub').hide();
									$('fp_scq').show();
									$('fp_sca').show();
									$('fp_scqa_sub').show();
									$('scq').update(resSP[0]);
									$('osca').value = resSP[1];
									$('ue').value = fpe;
									$('fp_email').value = "";
									$('fp_email_error').update("");
								}
							}
						 });
	}
}

var fp_check_security = function(){
	var bool = true;
	$('fp_sca_write_error').update("");
	var fpe = $('ue').value;
	if($('fp_sca_write').value.blank()){
		$('fp_sca_write_error').update("Required");
		bool = false;
	}else{
		if($('fp_sca_write').value != $('osca').value){
			$('fp_sca_write_error').update("Wrong data");
			bool = false;
		}
	}
	
	if(bool){
		var fp_cs_url = "ajaxserver/user/fp_check_security.php";
		$('fp_sca_fire_rotater').show();
		$('fp_sca_fire_button').hide();
		new Ajax.Request(fp_cs_url, {
						 	method : 'get',
							parameters : { e : fpe  },
							onSuccess : function(transport){
								var res = transport.responseText;
								//alert(res);
								var resSP = res.split("|");
								$('fp_sca_fire_rotater').hide();
								$('fp_sca_fire_button').show();
								if(resSP[0] == 1){
									$('fp_sca_write_error').update(res[1]);
								}else{
									$("fp_success").update("Your username and password is forwarded to your email address.<br /> Please check");
								}								
								setTimeout("$('fp_spinner').hide(); $('fp_link').show(); $('fp_success').update(''); create_modal('','','',false);", 5000);
							},
							onFailure : function(transport){
								alert("Server not Connected");
							}
						 });
	}
}
