function isNumeric(value) {

  if (value == null || !value.toString().match(/^[-]?\d*\.?\d*$/)) {
	alert("Enter numeric value")

	return false;
  }
  return true;
}

function allCheck(name, type, id, matchId){
	var matchValue = '';
	if(matchId != null) {
		matchValue = document.getElementById(matchId).value;
	}
	var pattern;
	var obj = document.getElementById(id);
	var content = obj.value;
	//alert(content);
	if(type=="email"){
		//alert(type);
		pattern="^[a-zA-Z0-9\-\_\.]+\@[a-zA-Z0-9\-\_\.]+\.([a-zA-Z]{2,4})$";
	}else if(type=="alphanumeric"){
		//alert(type);
		pattern="^[0-9a-zA-Z\-\.\,\/\#\'\"\:\ ]+$";
	}else if(type=="alpha"){
		//alert(type);
		pattern="^[a-zA-Z\ ]+$";
	}else if(type=="phone")
		pattern="^[0-9]{10}$";
	}else if(type=="zip"){
		//alert(type);
		pattern="^[0-9]{6}$";
	}else if(type=="number"){
		//alert(type);
		pattern="^[0-9]+$";
	}else if(type=="pwd"){
		pattern=/^\S{1,20}$/;
	}
	if(content == "" || content.length == 0){
		errorMessage = "Enter your "+name+".\n"
		document.getElementById("errorMessageDiv").innerHTML="<font color='red'><img src=images/c_err.gif width=13 height=13 align=absmiddle />"+errorMessage+"</font>";
		return false;
	}else if(!content.match(pattern)){
		errorMessage = name +" format is incorrect."
		document.getElementById("errorMessageDiv").innerHTML="<font color='red'><img src=images/c_err.gif width=13 height=13 align=absmiddle />"+errorMessage+"</font>";	
		return false;
	} else  if(matchId != null && matchValue != content){
		errorMessage = "Passwords are not matching."
		document.getElementById("errorMessageDiv").innerHTML="<font color='red'><img src=images/c_err.gif width=13 height=13 align=absmiddle />"+errorMessage+"</font>";	
		return false;
	}
	document.getElementById("errorMessageDiv").innerHTML="";
	return true;
}

var counter=0
var errorMessage = "";

//This function is used to get the total estimated cost for photos - By Raja , Date 08/12/2010
function photomultiply()
{
	if(allCheck('Estimated Photo quantity', 'number', 'photoorderquantity')){
		var totalordercost = 0;
		var photototal = 0;
		var photoquantity = document.getElementById("photoorderquantity").value;	 //To get the total quantity
		var photoordercost = parseFloat(document.getElementById("photoordercosthidden").value); //To get the previous order cost
		var photototal = photoquantity*9;                                           //To get the total   
		document.getElementById("photoordercost").innerHTML = photototal;           
		document.getElementById("photoordercosthidden").value = photototal;
		var totalordercost = photototal + parseFloat(document.getElementById("totalordercosthidden").value) - photoordercost;
		document.getElementById("totalordercosthidden").value = totalordercost;
		document.getElementById("totalordercost").innerHTML = totalordercost;
		document.getElementById("minimumamountpayable").innerHTML = 500;
		//document.getElementById("totalEstimateddcost").innerHTML = totalordercost;
		//document.getElementById("shippingbillingaddress").style.visibility="visible";
	}

}

//This function is used to get the total estimated cost for videos - By Raja , Date 08/12/2010
function videomultiply()
{
	if(allCheck('Estimated Video quantity', 'number', 'videoorderquantity')){
		 var totalordercost = 0;
		 var videototal = 0;
		 var videoquantity = document.getElementById("videoorderquantity").value;	
		 
		 var videoordercost = parseFloat(document.getElementById("videoordercosthidden").value); //To get the previous order cost
		 var videototal = videoquantity*250;
		 document.getElementById("videoordercost").innerHTML = videototal;
		 document.getElementById("videoordercosthidden").value = videototal;
		 var totalordercost = videototal + parseFloat(document.getElementById("totalordercosthidden").value) - videoordercost;
		 document.getElementById("totalordercosthidden").value = totalordercost;
		 document.getElementById("totalordercost").innerHTML = totalordercost;
 		 document.getElementById("minimumamountpayable").innerHTML = 500;		 
		 //document.getElementById("totalEstimateddcost").innerHTML = totalordercost;
	}
}


//This function is used to enable & disable the video and photo values

function photoenable()
{
	if (document.getElementById("selectphotos").checked)
    	 document.getElementById("photoorderquantity").readOnly=false;
		 
		 
	if (!document.getElementById("selectphotos").checked) 
    	 document.getElementById("photoorderquantity").readOnly=true;
		 document.getElementById("photoorderquantity").value = '0';
		 if(document.getElementById("photoordercost").innerHTML != '') {
			 photomultiply();
			 document.getElementById("photoordercosthidden").value = 0;
		 }
}

function videoenable()
{
	if (document.getElementById("selectvideos").checked)
    	 document.getElementById("videoorderquantity").readOnly=false;

	if (!document.getElementById("selectvideos").checked)
    	 document.getElementById("videoorderquantity").readOnly=true;
		 document.getElementById("videoorderquantity").value = '0';
		 if(document.getElementById("videoordercost").innerHTML != '') {
			 videomultiply();
			 document.getElementById("videoordercosthidden").value = 0;
		 }
}

function firstnameupdate(type)
{
	if(allCheck(type+' first name', 'alpha', type+'firstname')){
		if(!document.getElementById("shipAtBillingCheckBox").checked){
			document.getElementById("billingfirstname").value = document.getElementById("shippingfirstname").value;
			document.getElementById("billingfirstname").readOnly="readonly";
		}else if(type=="shipping"){
			document.getElementById("billingfirstname").value = "";
			document.getElementById("billingfirstname").readOnly=false;
		}
	}
}

function lastnameupdate(type)
{
	if(allCheck(type+' last name', 'alpha', type+'lastname')){
		if(!document.getElementById("shipAtBillingCheckBox").checked){
			document.getElementById("billinglastname").value = document.getElementById("shippinglastname").value;
			document.getElementById("billinglastname").readOnly="readonly";
		}else if(type=="shipping"){
			document.getElementById("billinglastname").value = "";
			document.getElementById("billinglastname").readOnly=false;
		}
	}
}

function addressupdate(type)
{
	if(allCheck(type+' address', 'alphanumeric', type+'address')){
		if(!document.getElementById("shipAtBillingCheckBox").checked){
			document.getElementById("billingaddress").value = document.getElementById("shippingaddress").value;
			document.getElementById("billingaddress").readOnly="readonly";
		}else if(type=="shipping"){
			document.getElementById("billingaddress").value = "";
			document.getElementById("billingaddress").readOnly=false;
		}
	}
}

function cityupdate(type)
{
	if(allCheck(type+' city', 'alpha', type+'city')){
		if(!document.getElementById("shipAtBillingCheckBox").checked){
			document.getElementById("billingcity").value = document.getElementById("shippingcity").value;
			document.getElementById("billingcity").readOnly="readonly";
		}else if(type=="shipping"){
			document.getElementById("billingcity").value = "";
			document.getElementById("billingcity").readOnly=false;
		}
	}
}

function stateupdate(type)
{
	if(allCheck(type+' state', 'alpha', type+'state')){
		if(!document.getElementById("shipAtBillingCheckBox").checked){
			document.getElementById("billingstate").value = document.getElementById("shippingstate").value;
			document.getElementById("billingstate").readOnly="readonly";
		}else if(type=="shipping"){
			document.getElementById("billingstate").value = "";
			document.getElementById("billingstate").readOnly=false;
		}
	}
}

function zipcodeupdate(type)
{
	if(allCheck(type+' zip code', 'zip', type+'zipcode')){
		if(!document.getElementById("shipAtBillingCheckBox").checked){
			document.getElementById("billingzipcode").value = document.getElementById("shippingzipcode").value;
			document.getElementById("billingzipcode").readOnly="readonly";
		}else if(type=="shipping"){
			document.getElementById("billingzipcode").value = "";
			document.getElementById("billingzipcode").readOnly=false;
		}
	}
}

function phoneupdate(type)
{
	if(allCheck(type+' phone number', 'phone', type+'phone')){
		if(!document.getElementById("shipAtBillingCheckBox").checked){
			document.getElementById("billingphone").value = document.getElementById("shippingphone").value;
			document.getElementById("billingphone").readOnly="readonly";
		}else if(type=="shipping"){
			document.getElementById("billingphone").value = "";
			document.getElementById("billingphone").readOnly=false;
		}
	}
}

function populateBillingDetails(){
	firstnameupdate('shipping');
	lastnameupdate('shipping');
	addressupdate('shipping');
	cityupdate('shipping');
	stateupdate('shipping');
	zipcodeupdate('shipping');
	phoneupdate('shipping');
}

function togglelogin()
{
	document.getElementById('logintoaccount').style.display = '';
	document.getElementById('createanaccount').style.display = 'none';
}

function toggleaccount()
{
	document.getElementById('createanaccount').style.display = '';
	document.getElementById('logintoaccount').style.display = 'none';
}

function mul1(x,y,a)
{
  var qty="qty"+y;
  var total="total"+y;
  var weight="weight"+y;
  var weights="weights"+y;
  var text="text"+y;
  var z=document.getElementById(qty).value;
  isNumeric(z)
  var total1=z*x;
  var totalw=z*a;
  document.getElementById(text).innerHTML="INR  "+truncate(total1);
  //document.getElementById(weight).innerHTML=totalw;
  document.getElementById(total).value=total1;
  document.getElementById(weights).value=totalw;
  var gtotal=document.getElementById(total).value
  //counter=counter+parseFloat(gtotal)
  submit_form()
  }
  
 
function submit_form()
	{        var total=0
	         var totqty=0

		     dml=document.forms['form1'];
		// get the number of elements from the document
 		 	 len = dml.elements.length;  // 15
			 
			 for( i=0 ; i<len ; i++)   // loop is run 15 times
			 {
				 
				if(dml.elements[i].name=='qty[]')
				{
					    dml.elements[i].focus();
 						var totqty = totqty + parseFloat(dml.elements[i].value)					
				}
				
				//check the textbox with the elements name
				if (dml.elements[i].name=='total[]')
				{
				        dml.elements[i].focus();
						var total= total + parseFloat(dml.elements[i].value)
			     }
				
			 }
		
		 //alert(total)
		document.getElementById("Sum").innerHTML="INR  "+truncate(total);
		document.getElementById("totqty").innerHTML=totqty;
		document.getElementById("gtotal").value=truncate(total);	
		document.getElementById("totalquantity").value=truncate(totqty);
	}
	
function truncate(num) {
		string = "" + num;
		if (string.indexOf('.') == -1)
		return string + '.00';
		seperation = string.length - string.indexOf('.');
		if (seperation > 3)
		return string.substring(0,string.length-seperation+3);
		else if (seperation == 2)
		return string + '0';
		return string;
} 
	
function clearcontent(id, defaultValue)
{
	var temp = document.getElementById(id).value;
	
	if(temp == 'Provide a password' || temp == 'Enter your password again' || temp == 'Password should be same')
	{
		document.getElementById(id).value = "";
		document.getElementById(id).type = "password";	
	}
	
	if(temp == 'Provide verification code' || temp == 'Invalid email-id')
	{
		document.getElementById(id).value = "";
	}
	
	if(document.getElementById(id).value == defaultValue){
		document.getElementById(id).value = "";
		if(id == 'password' || id == 'reenterpassword') { 
			document.getElementById(id).type = "password";
		}
	}
	
}

function fillcontent(id, defaultValue)
{
	if(document.getElementById(id).value == ""){
		if(id == 'password' || id == 'reenterpassword') { 
			document.getElementById(id).type = "text";
		}
		document.getElementById(id).value = defaultValue;	
	}
}

function testfunction()
{
	var path = document.getElementById("uploadimage").value;	
	alert(path);
}

function printDiv(id)
{

	content = document.getElementById(id).innerHTML;
	var newWin=window.open('','PrintWindow','width=1024,height=400');
	newWin.document.open();
	newWin.document.write('<html><body>'+content+'</body></html>');
	newWin.document.close();
    newWin.window.print();
	setTimeout(function(){newWin.close();},10);
}
