function openNewWindow(src,width,height) {
	propertyString="width="+width+",height="+height;
	window.open("/open-ring-picture.php?src="+src,"newwindow",propertyString);
}

function contactCheck(f) {
	if (f.email.value.indexOf("@") + "" == "-1" || f.email.value.indexOf(".") + "" == "-1" || f.email.value == "" )
	 { 
		alert("Please check your email address.");
		f.email.focus();
		return false;
	 }
	 return true;
}

function checkCart(f) {
	var reg1= /[^0-9]/;	// regular expression
	for (i=0;i<f.elements.length;i++)
	{
		if (f.elements[i].name.search("cID:") != -1)	 // if it finds the match
		{
			if ( reg1.test(f.elements[i].value) )
			{
				alert ("Error Found in the Qyu field.");
				f.elements[i].value=1;
				f.elements[i].focus();
				return false;
			}
		}
	}
}

function shippingAddress(f) {
         if (f.profilename.value.length < 1) {
                alert("Please enter shipping name.");
				f.profilename.focus(); 
                return false;
         }

         if (f.address1.value.length < 1) {
                alert("Please enter at least one street address.");
				f.address1.focus(); 
                return false;
         }

         if (f.city.value.length < 1) {
                alert("Please enter city.");
				f.city.focus(); 
                return false;
         }
        
 		 if (f.zip.value.length < 1 )
		 {
                alert("Please enter zipcode.");
				f.zip.focus(); 
                return false;             
         }
            
}

function billingAddress(f) {
         if (f.fname.value.length < 1) {
                alert("Please enter first name.");
				f.fname.focus(); 
                return false;
         }

         if (f.lname.value.length < 1) {
                alert("Please enter last name.");
				f.lname.focus(); 
                return false;
         }		 

         if (f.address1.value.length < 1) {
                alert("Please enter street address.");
				f.address1.focus(); 
                return false;
         }

         if (f.city.value.length < 1) {
                alert("Please enter city.");
				f.city.focus(); 
                return false;
         }
        
 		 if (f.zip.value.length < 1 )
		 {
                alert("Please enter zipcode.");
				f.zip.focus(); 
                return false;             
         }
            
         if (f.phone.value.length < 1) {
                alert("Please enter phone number.");
				f.phone.focus(); 
                return false;
         }
}

function copyAddress () {
	var f=document.getElementById("create");
	if ( document.getElementById("same").checked)
	{
		f.sAddress1.value=f.address1.value;
		f.sAddress2.value=f.address2.value;
		f.sCity.value=f.city.value;
		f.sState.selectedIndex=f.state.selectedIndex;
		f.sZip.value=f.zip.value;
	}
	else
	{
		f.sAddress1.value="";
		f.sAddress2.value="";
		f.sCity.value="";
		f.sState.selectedIndex=0;
		f.sZip.value="";
	}
}

function updateTotal() {
	if ( document.getElementById("shippingMethod") ) {
	var shippingCost=document.getElementById("shippingMethod").options[document.getElementById("shippingMethod").selectedIndex].value;
	var shipping=shippingCost.split(":");
	var discount=parseFloat(document.getElementById("couponDiscount").value);
	var newTotal=parseFloat(document.getElementById("subtotal").value)+parseFloat(shipping[1])-discount;
	var tax=parseFloat(document.getElementById("tax").value);

	newTotal=newTotal+tax;

//	newTotal=Math.round(newTotal*100)/100;
	document.getElementById("totalBanner").innerHTML="Total: $"+newTotal.toFixed(2);
	document.getElementById("total").value=newTotal;
	document.getElementById("shippingRate").value=parseFloat(shipping[1]);

	}
}

//sub-function for checking up the credit card number only
function numberChecker(form) {
	if (form.cardNum.value.length<13) return false; //no creditcard length is smaller than 13

	//length check
    if (form.cardNum.value.length!=15 && form.cctype.selectedIndex == 0) return false; //amex
    if (form.cardNum.value.length!=13 && form.cardNum.value.length!=16 && form.cctype.selectedIndex == 1) return false; //visa
    if (form.cardNum.value.length!=16 && form.cctype.selectedIndex == 2) return false; //mastercard
	if (form.cardNum.value.length!=16 && form.cctype.selectedIndex == 3) return false; //discover

	//front number check
	cardFrontDigit=form.cardNum.value.substring(0,1);
	if (form.cctype.selectedIndex == 0 && cardFrontDigit!="3") return false; //amex
	if (form.cctype.selectedIndex == 1 && cardFrontDigit!="4") return false; //visa
	if (form.cctype.selectedIndex == 2 && cardFrontDigit!="5") return false; //master
	if (form.cctype.selectedIndex == 3 && cardFrontDigit!="6") return false; //discover


	//check the number validation by using mod algorithm
	cardCheckSum=new Array(16);
	for (i=0;i<form.cardNum.value.length;i++)
		cardCheckSum[i]=form.cardNum.value.substring(i,i+1);
	var finalCheckSum=0;
	
	for (i = (form.cardNum.value.length % 2); i < form.cardNum.value.length; i+=2) 
	{
		var a=cardCheckSum[i]*2;
		 
		if (a>=10) { 
			var aStr = a.toString();
			var b = aStr.substring(0,1);
			var c = aStr.substring(1,2);
			cardCheckSum[i] = Math.floor(b) + Math.floor(c);
		}
		else {
		cardCheckSum[i] =a;
		}
	}
	for (i = 0; i < form.cardNum.value.length; i++)
	finalCheckSum += Math.floor(cardCheckSum[i]);
	
	if (finalCheckSum%10!=0) //if the number is divided by 10, the card number is valid
        return false;
    else
		return true;
}

//main fucntion to check and validiate all the information
function cardCheck(form) {
	if ( document.getElementById("dupChecker").value != 1 ) 
		return false;

//check if all information is filled
	if (!form.nameOnCard.value) {
		alert("Enter the name on credit card.");
		form.nameOnCard.focus();
		document.getElementById("dupChecker").value=0;
		return false; }
	if (!form.cardNum.value) {
		alert("Enter a credit card number.");
		form.cardNum.focus();
		document.getElementById("dupChecker").value=0;
		return false; }

	if (!form.cvv2.value ) {
		alert("Please enter the CVV Code from your credit card.");
		form.cvv2.focus();
		document.getElementById("dupChecker").value=0;
		return false; }
	
//call number checking function
    if (!numberChecker(form)) 
	{
    alert("Card number is not valid.");
    form.cardNum.focus();
	document.getElementById("dupChecker").value=0;
    return false;
    }
}

function displayCVV(arg) {
	if (arg == 1)
		document.getElementById("cvvImage").style.display="none";
	else
		document.getElementById("cvvImage").style.display="";
}


function dupCheck() {
	document.getElementById("dupChecker").value++;
}
