function formsubmit(e) {
if (!e) var e = window.event;
if (e.keyCode) code = e.keyCode;
else if (e.which) code = e.which;
//alert(code);
if (code==13) {
login_new();

}
}
function login_new()
{
	$("#msg").removeClass().addClass("info").text("Checking..").fadeIn("slow");
	$.post("ajax.php",{action:'login',email:$("#email").val(),pass:$("#pass").val()}, function(logindata)
	{
	//alert(logindata);
		if(logindata=='OK')
		{	
			goto('onestepcheckout.php');
			/*
			$("#msg").remove();			
			$("#loginbox").ramove();*/
		}
		else if(logindata=='INVALID')
		{
			$("#msg").removeClass().addClass("error").text("Invalid email address or password").fadeIn("slow");
		}
		else if(logindata=='NOT VERIFIED')
		{
			$("#msg").removeClass().addClass("error").text("Your account is not verified.").fadeIn("slow");
		}
		else if(logindata=='BANNED')
		{
			$("#msg").removeClass().addClass("error").text("Your account is banned").fadeIn("slow");
		}
	});

}

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){
		   alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert("Invalid E-mail ID")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("Invalid E-mail ID")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    alert("Invalid E-mail ID")
		    return false
		 }

 		 return true					
	}



function calshipping()
{
	$("#loader").show();
	$.post('ajax.php',{action:'calshipping',scountry:$("#scountry").val(),szip:$("#szip").val(),sstate:$("#sstate").val()},function(data){
																																	xdata = data.split("_");
	$("#tax").text(number_format(xdata[0],2));
	$("#shipping").text(number_format(xdata[1],2));
	$("#total").text(number_format(xdata[2],2));
	$("#subtotal").text(number_format(xdata[3],2));
	$("#loader").hide();
	});
}

function validate_checkout()
{
	var pmethod = $("input[name='pmethod']:checked").val();

	if($("#bfname").val()=='')
	{
		alert("Please enter your billing first name");	
		return false;
	}
	if($("#blname").val()=='')
	{
		alert("Please enter your billing last name");	
		return false;
	}
	if($("#baddress").val()=='')
	{
		alert("Please enter your billing address");	
		return false;
	}
	if($("#bcity").val()=='')
	{
		alert("Please enter your billing city");	
		return false;
	}
	if($("#bstate").val()=='')
	{
		alert("Please enter your billing state");	
		return false;
	}
	if($("#bcountry").val()=='')
	{
		alert("Please enter your billing country");	
		return false;
	}
	if($("#bzip").val()=='')
	{
		alert("Please enter your billing zip code");	
		return false;
	}
	if($("#cemail").val()=='')
	{
		alert("Please enter your email address");	
		return false;
	}
	if($("#telephone").val()=='')
	{
		alert("Please enter your phone number");	
		return false;
	}
	if (echeck($("#cemail").val())==false){
		
		return false
	}
	if($("#sfname").val()=='')
	{
		alert("Please enter your shipping first name");	
		return false;
	}
	if($("#slname").val()=='')
	{
		alert("Please enter your shipping last name");	
		return false;
	}
	if($("#saddress").val()=='')
	{
		alert("Please enter your shipping address");	
		return false;
	}
	if($("#scity").val()=='')
	{
		alert("Please enter your shipping city");	
		return false;
	}
	if($("#sstate").val()=='')
	{
		alert("Please enter your shipping state");	
		return false;
	}
	if($("#scountry").val()=='')
	{
		alert("Please enter your shipping country");	
		return false;
	}
	if($("#szip").val()=='')
	{
		alert("Please enter your shipping zip code");	
		return false;
	}	

	if($("#createacc").is(':checked'))
	{
		
		if($("#npass").val()=='')
		{
			alert("Please enter your password")	;
			return false;
		}
		if($("#cpass").val()!=$("#npass").val())
		{
			alert("Confirm password is not matching")	;
			return false;
		}

		if(pmethod=='ccard')
		{
			

			if($("#ccname").val()=='')
			{
				alert("Please enter your name");
				return false;
			}
			if($("#ccnumber").val()=='')
			{
				alert("Please enter your Credit Card Number");
				return false;
			}
			if($("#month").val()=='')
			{
				alert("Please select expiry month");
				return false;
			}
					
		}
	}	
	if(pmethod=='ccard')
		{
			if($("#ccname").val()=='')
			{
				alert("Please enter your name");
				return false;
			}
			if($("#ccnumber").val()=='')
			{
				alert("Please enter your Credit Card Number");
				return false;
			}
			if($("#month").val()=='')
			{
				alert("Please select expiry month");
				return false;
			}
					
		}
		
	$('#paynowbut').hide();
	return true;
}

function showcreateaccbox()
{
	if( $("#createacc").is(':checked') )
	$("#createaccbox").show();
	else
	$("#createaccbox").hide();


}

function showshipbox()
{
	if( $("#diffship").is(':checked') )
	$("#shipbox").show();
	else
	$("#shipbox").hide();


}


function attribute_price()
{

	var av =parseFloat($("#price_product").val());
	
$(".Dropdown option:selected").each(function(x){
	av+=parseFloat($(this).attr('price'));
	});

	$(".Radio:checked").each(function(x){
	
	av+=parseFloat($(this).attr("price"));
	
	});
	
	
	$(".Checkbox:checked").each(function(x){
	
	av+=parseFloat($(this).attr("price"));
	});
	
	$("#price").text(number_format(av,2));
	$("#price2").text(number_format(av,2));
	
	$("#paymentAmount").val(  number_format(av,2) );
	$("#item_price_1").val( number_format(av,2) );
}

function number_format(pnumber,decimals){
	if (isNaN(pnumber)) { return 0};
	if (pnumber=='') { return 0};
	
	var snum = new String(pnumber);
	var sec = snum.split('.');
	var whole = parseFloat(sec[0]);
	var result = '';
	
	if(sec.length > 1){
		var dec = new String(sec[1]);
		dec = String(parseFloat(sec[1])/Math.pow(10,(dec.length - decimals)));
		dec = String(whole + Math.round(parseFloat(dec))/Math.pow(10,decimals));
		var dot = dec.indexOf('.');
		if(dot == -1){
			dec += '.'; 
			dot = dec.indexOf('.');
		}
		while(dec.length <= dot + decimals) { dec += '0'; }
		result = dec;
	} else{
		var dot;
		var dec = new String(whole);
		dec += '.';
		dot = dec.indexOf('.');		
		while(dec.length <= dot + decimals) { dec += '0'; }
		result = dec;
	}	
	return result;
}



function loadpage(page)
{
	$("#loader").fadeIn("fast");
	$("#pagebox").fadeOut("fast");
	$("#pagebox").empty();
	$("#pagebox").load(page,{},function(){
			$("#pagebox").fadeIn("slow");
			$("#loader").fadeOut(800);
			
	});

}

function setVisibility1() 
{
	document.getElementById('schange').style.display = "none";	
	document.getElementById('scancel').style.display = "inline";	
	document.getElementById('saddresstab').style.display = "inline";	
	document.getElementById('oad1').style.display = "none";	

}
function setVisibility2() 
{
	
	document.getElementById('scancel').style.display = "none";	
	document.getElementById('schange').style.display = "inline";	
	document.getElementById('saddresstab').style.display = "none";	
	document.getElementById('oad1').style.display = "inline";	
	
	document.getElementById('snamex1').innerHTML=document.getElementById('sfname').value;
	document.getElementById('slnamex1').innerHTML=document.getElementById('slname').value;
	document.getElementById('saddressx1').innerHTML=document.getElementById('saddress').value;
	document.getElementById('scityx1').innerHTML=document.getElementById('scity').value;
	document.getElementById('sstatex1').innerHTML=document.getElementById('sstate').value;
	document.getElementById('szipx1').innerHTML=document.getElementById('szip').value;
	document.getElementById('scountryx1').innerHTML=document.getElementById('scountry').value;
}


function setVisibility3() 
{
	document.getElementById('bchange').style.display = "none";	
	document.getElementById('bcancel').style.display = "inline";	
	document.getElementById('baddresstab').style.display = "inline";	
	document.getElementById('oad2').style.display = "none";	

}
function setVisibility4() 
{
	
	document.getElementById('bcancel').style.display = "none";	
	document.getElementById('bchange').style.display = "inline";	
	document.getElementById('baddresstab').style.display = "none";	
	document.getElementById('oad2').style.display = "inline";	
	
	document.getElementById('bnamex1').innerHTML=document.getElementById('bfname').value;
	document.getElementById('blnamex1').innerHTML=document.getElementById('blname').value;
	document.getElementById('baddressx1').innerHTML=document.getElementById('baddress').value;
	document.getElementById('bcityx1').innerHTML=document.getElementById('bcity').value;
	document.getElementById('bstatex1').innerHTML=document.getElementById('bstate').value;
	document.getElementById('bzipx1').innerHTML=document.getElementById('bzip').value;
	document.getElementById('bcountryx1').innerHTML=document.getElementById('bcountry').value;
}


function submitform(formx)
{
	var id = document.getElementById(formx);
	id.submit();
}


function submitformaction(formx,action)
{
	var id = document.getElementById(formx);
	id.action=action;
	id.submit();
}

function yearlist(mid)
{
	//$("#year_loader").fadeIn(100);
	$.post("ajax.php",{action:"yearlist",model_id:mid},function(data)
	{
		$("#year").empty();
		$("#year").append(data);
		//$("#year_loader").fadeOut(100);	
	});
}

function modellist(cid)
{
	//$("#model_loader").fadeIn(100);
	$.post("ajax.php",{action:"modellist",company_id:cid},function(data)
	{
		$("#model_id").empty();
		$("#model_id").append(data);
		//$("#model_loader").fadeOut(100);	
	
	
	});

}


function updateall(pid)
{
var type = new Array("p","m","r","w","s");
var i =0;
while(i<5)
{
	//alert(pid+"/"+type[i]);
	setprice(pid,type[i]);
	i++;
}

}

function setprice(pid,type)
{
	var wtext = "#"+type+pid;
	var div = "#div"+type+pid;
	var p = "#p"+type+pid;
	if($(wtext).val()!="")
	{
		$.post("ajax.php",{action:'updateprice', pid:pid, type:type, price:$(wtext).val() }, function(datap)
		{
		$(div).hide();
		$(p).text(datap);
		}
		);
	}
	else if($(wtext).val()=="")
	{
	$(div).hide();
	}
	
}



function gettd(pid,type)
{
	var div = "#div"+type+pid;
	var wtext = "#"+type+pid;
	$(div).show();	
	$(wtext).focus();
}
function hidediv(pid,type)
{

	var div = "#div"+type+pid;
	$(div).hide();	
	
}


function goto(url)
{
	
		window.location=url;
	
}

function del(url)
{
	if(	confirm("Are you sure you want to delete it?") )
	{
		window.location=url;
	}
}
