function checkQuantity(tmpStr)
{
	var i = 0;
    for (i = 0; i < tmpStr.length; ++i)
	if (!((tmpStr.charAt(i) >= "0" && tmpStr.charAt(i) <= "9")
		)) return true;
    return false;
}
//function to check the quantity 
function CheckDecimal(txtVal)
{
	if(checkQuantity(txtVal))
	{
		alert('Quantity should be numeric.');
		return false;
	}
}
function max_total(tot, noOfBox, obj_id)
{
	var i=1;
	var total=0;
	for (i=1; i<= noOfBox; i++)
	{
		CheckDecimal(document.getElementById(obj_id+"_"+i).value);
		if( (document.getElementById(obj_id+"_"+i).value - 0 ))
		total = total + (document.getElementById(obj_id+"_"+i).value-0);
	}
	if(total > tot)
	{
		alert("Total no should not exceed "+tot+".");
		return false;
	}
	return true;
}


function max_total_x(tot, noOfBox, obj_id, tot2, noOfBox2, obj_id2,objname )
{
	var i=1;
	var total=0;
	var total1=0;
	for (i=1; i<= noOfBox; i++)
	{
		if( (document.getElementById(obj_id+"_"+i).value - 0 ))
		total = total + (document.getElementById(obj_id+"_"+i).value-0);
	}
	for (i=1; i<= noOfBox2; i++)
	{
		if( (document.getElementById(obj_id2+"_"+i).value - 0 ))
		total1 = total1 + (document.getElementById(obj_id2+"_"+i).value-0);
	}

	if(total%2 != 0)
	{
		if(objname == 'Shelf')
		{
			//alert("Total no of "+objname+" Door should be half of total "+objname+" sets.");
			alert('You need to order 1 door set for every 2 shelf sets.');
			return false;
		}
		else if(objname == 'Cube')
		{
			alert('You need to order 2 door set for every 2 cube sets.');
			return false;
		}
		//alert("Total no of "+objname+" sets should be even.");
		//return false;
	}
	else if(objname == 'Shelf')
	{
		if(total/2 != total1)
		{
			//alert("Total no of "+objname+" Door should be half of total "+objname+" sets.");
			alert('You need to order 1 door set for every 2 shelf sets.');
			return false;
		}
		else
		{
			return true;
		}
	}
	else if(objname == 'Cube')
	{
		if(total != total1)
		{
			//alert("Total no of "+objname+" Door should be equal to total "+objname+" sets.");
			alert('You need to order 2 door set for every 2 cube sets.');
			return false;
		}
		else
		{
			return true;
		}
		
	}
	else
	{
		return true;
	}
}



function formValidSpecial()
{
	if( !max_total_x(1000, 3,'txtQtySpl_1', 500, 6,'txtQtySpl_5','Cube') 
	 || !max_total_x(1000, 3,'txtQtySpl_2', 500, 6,'txtQtySpl_6','Shelf') )
	{
		//alert("You have not specified the correct quantities in some products. \n\nPlease correct the quantities of products as per direction.");
		return false;
	}
	else
	{
		return true;
	}
}


function copyvalue()
{
	document.getElementById('txtFirstNameBill').value = document.getElementById('txtFirstNameShip').value;
	document.getElementById('txtStreetBill').value = document.getElementById('txtStreetShip').value;
	document.getElementById('txtCityBill').value = document.getElementById('txtCityShip').value;
	document.getElementById('txtStateBill').value = document.getElementById('txtStateShip').value;
	document.getElementById('txtZipBill').value = document.getElementById('txtZipShip').value;
	document.getElementById('txtCountryBill').value = document.getElementById('txtCountryShip').value;
	return true;
}

function formValidatorShip(theForm)
{
	if (theForm.txtFirstNameShip.value == "")
	{
		alert("Shipping Name cannot be blank!");
		theForm.txtFirstNameShip.focus();
		return (false);
	}

	if (theForm.txtStreetShip.value == "")
	{
		alert("Shipping Street Address cannot be blank!");
		theForm.txtStreetShip.focus();
		return (false);
	}

	if (theForm.txtCityShip.value == "")
	{
		alert("Shipping City cannot be blank!");
		theForm.txtCityShip.focus();
		return (false);
	}


	if (theForm.txtStateShip.value == "")
	{
		alert("Shipping state cannot be blank!");
		theForm.txtStateShip.focus();
		return (false);
	}
	else if (theForm.txtStateShip.value == "--" && theForm.txtCountryShip.value == "US")
	{
		alert("Invalid Shipping state !");
		theForm.txtStateShip.focus();
		return (false);
	}

	if (theForm.txtZipShip.value == "")
	{
		alert("Shipping Zip code cannot be blank!");
		theForm.txtZipShip.focus();
		return (false);
	}
	else if(!validateZIP(theForm.txtZipShip.value) )
	{
		theForm.txtZipShip.focus();
		return (false);	  
	}

	if (theForm.txtEmailShip.value == "")
	{
		alert("e-Mail Address cannot be blank!");
		theForm.txtEmailShip.focus();
		return (false);
	}
		
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (!filter.test(theForm.txtEmailShip.value))
	{
		alert("Invalid Email Address!");
		theForm.txtEmailShip.focus();
		return (false);
	}
	if (!theForm.optPayType[0].checked && !theForm.optPayType[1].checked)
	{
		alert("Please select a payment mode!");
		theForm.optPayType[0].focus();
		return (false);
	}
	
	if (theForm.txtPromoCode.value != "" && ( theForm.txtPromoCode.value != "OCE06" && theForm.txtPromoCode.value != "CKE06") )
	{
		alert("Invalid Promo Code!");
		theForm.txtPromoCode.focus();
		return (false);
	}
	
	return true;
}


function formValidatorBill(theForm)
{
	if (!theForm.chkSameAsShipping.checked)
	{
		if (theForm.txtFirstNameBill.value == "")
		{
			alert("Billing Name cannot be blank!");
			theForm.txtFirstNameBill.focus();
			return (false);
		}

		if (theForm.txtStreetBill.value == "")
		{
			alert("Billing Street Address cannot be blank!");
			theForm.txtStreetBill.focus();
			return (false);
		}

		if (theForm.txtCityBill.value == "")
		{
			alert("Billing City cannot be blank!");
			theForm.txtCityBill.focus();
			return (false);
		}


		if (theForm.txtStateBill.value == "")
		{
			alert("Billing state cannot be blank!");
			theForm.txtStateBill.focus();
			return (false);
		}
		else if (theForm.txtStateBill.value == "--" && theForm.txtCountryBill.value == "US")
		{
			alert("Invalid Billing state !");
			theForm.txtStateBill.focus();
			return (false);
		}

		if (theForm.txtZipBill.value == "")
		{
			alert("Billing Zip code cannot be blank!");
			theForm.txtZipBill.focus();
			return (false);
		}
		else if(!validateZIP(theForm.txtZipBill.value) )
		{
			theForm.txtZipBill.focus();
			return (false);	  
		}
		
	}
	
	if (theForm.cc_type.value == "")
	{
		alert("Please chose a Credit Card Type!");
		theForm.cc_type.focus(); 
		return (false);
	}

	if (theForm.cc_number.value == "")
	{
		alert("Credit Card Number cannot be blank!");
		theForm.cc_number.focus();
		return (false);
	}  

	if (theForm.cc_csc.value == "")
	{
		alert("Credit Card ID Number cannot be blank!");
		theForm.cc_csc.focus();
		return (false);
	}  

	if (theForm.cc_expmonth.value == "")
	{
		alert("Credit Card expiry month cannot be blank!");
		theForm.cc_expmonth.focus();
		return (false);
	}  

	if (theForm.cc_expyear.value == "")
	{
		alert("Credit Card expiry year cannot be blank!");
		theForm.cc_expyear.focus();
		return (false);
	}  
	//compare the expiration date with today
	if(theForm.cc_expmonth.value != "" && theForm.cc_expyear.value != "")
	{
		today = new Date();
		//expiry = new Date(year, month);
		expiry = new Date(theForm.cc_expyear.value, theForm.cc_expmonth.value);
		if (today.getTime() > expiry.getTime())
		{
			alert("Expiration Date should be greater than Today.");
			return false;
		}
	}
	if(!CheckCardNumber(theForm))
	{
		theForm.cc_number.focus();
		return false;
	}

	if (theForm.cc_number.value == "")
	{
		alert("Credit Card Number cannot be blank!");
		theForm.cc_number.focus();
		return (false);
	}

	
	
	return true;
}

function validateZIP(field) 
{
	var valid = "0123456789-";
	var hyphencount = 0;
	
	if (field.length < 5 || (field.length > 5 && field.length < 10) || field.length > 10) 
	{
		alert("Please enter your 5 digit or 5+ zip code.");
		return false;
	}
	/*
	else
	{
		alert("Please enter your 5 digit or 5+ zip code.");
		return false;
	}
	*/
	for (var i=0; i < field.length; i++) 
	{
		temp = "" + field.substring(i, i+1);
		if (temp == "-") hyphencount++;
		if (valid.indexOf(temp) == "-1") 
		{
			alert("Invalid characters in your zip code.  Please try again.");
			return false;
		}
		if ((hyphencount > 1) || ((field.length==10) && ""+field.charAt(5)!="-")) 
		{
			alert("The hyphen character should be used with a properly formatted 5 digit+four zip code, like '12345-6789'.   Please try again.");
			return false;
		}
	}
	return true;
}



/*

Card Validation

*/


var Cards = new makeArray(5);
Cards[0] = new CardType("MasterCard", "51,52,53,54,55", "16");
var MasterCard = Cards[0];
Cards[1] = new CardType("Visa", "4", "13,16");
var Visa = Cards[1];
Cards[2] = new CardType("Amex", "34,37", "15");
var Amex = Cards[2];
Cards[3] = new CardType("Discover", "6011", "16");
var Discover = Cards[3];

var LuhnCheckSum = Cards[4] = new CardType();

/*************************************************************************\
CheckCardNumber(form)
function called when users click the "check" button.
\*************************************************************************/
function CheckCardNumber(form) {
var tmpyear;
if (form.cc_number.value.length == 0) {
alert("Please enter a Card Number.");
form.cc_number.focus();
return;
}
if (form.cc_expyear.value.length == 0) {
alert("Please enter the Expiration Year.");
form.cc_expyear.focus();
return;
}
if (form.cc_expyear.value > 96)
tmpyear = "19" + form.cc_expyear.value;
else if (form.cc_expyear.value < 21)
tmpyear = "20" + form.cc_expyear.value;
else {
alert("The Expiration Year is not valid.");
return;
}
tmpmonth = form.cc_expmonth.options[form.cc_expmonth.selectedIndex].value;
// The following line doesn't work in IE3, you need to change it
// to something like "(new CardType())...".
// if (!CardType().isExpiryDate(tmpyear, tmpmonth)) {
if (!(new CardType()).isExpiryDate(tmpyear, tmpmonth)) {
alert("This card has already expired.");
return;
}
card = form.cc_type.options[form.cc_type.selectedIndex].value;
var retval = eval(card + ".checkCardNumber(\"" + form.cc_number.value +
"\", " + tmpyear + ", " + tmpmonth + ");");
cardname = "";
if (retval)



// comment this out if used on an order form
//alert("This card number appears to be valid.");
return true;


else {
// The cardnumber has the valid luhn checksum, but we want to know which
// cardtype it belongs to.
for (var n = 0; n < Cards.size; n++) {
if (Cards[n].checkCardNumber(form.cc_number.value, tmpyear, tmpmonth)) {
cardname = Cards[n].getCardType();
break;
   }
}
if (cardname.length > 0) {
alert("This looks like a " + cardname + " number, not a " + card + " number.");
}
else {
alert("This card number is not valid.");
      }
   }
}
/*************************************************************************\
Object CardType([String cardtype, String rules, String len, int year, int month])
cardtype    : type of card, eg: MasterCard, Visa, etc.
rules       : rules of the cardnumber, eg: "4", "6011", "34,37".
len         : valid length of cardnumber, eg: "16,19", "13,16".
year        : year of expiry date.
month       : month of expiry date.
eg:
var VisaCard = new CardType("Visa", "4", "16");
var AmExCard = new CardType("AmEx", "34,37", "15");
\*************************************************************************/
function CardType() {
var n;
var argv = CardType.arguments;
var argc = CardType.arguments.length;

this.objname = "object CardType";

var tmpcardtype = (argc > 0) ? argv[0] : "CardObject";
var tmprules = (argc > 1) ? argv[1] : "0,1,2,3,4,5,6,7,8,9";
var tmplen = (argc > 2) ? argv[2] : "13,14,15,16,19";

this.setCardNumber = setCardNumber;  // set CardNumber method.
this.setCardType = setCardType;  // setCardType method.
this.setLen = setLen;  // setLen method.
this.setRules = setRules;  // setRules method.
this.setExpiryDate = setExpiryDate;  // setExpiryDate method.

this.setCardType(tmpcardtype);
this.setLen(tmplen);
this.setRules(tmprules);
if (argc > 4)
this.setExpiryDate(argv[3], argv[4]);

this.checkCardNumber = checkCardNumber;  // checkCardNumber method.
this.getExpiryDate = getExpiryDate;  // getExpiryDate method.
this.getCardType = getCardType;  // getCardType method.
this.isCardNumber = isCardNumber;  // isCardNumber method.
this.isExpiryDate = isExpiryDate;  // isExpiryDate method.
this.luhnCheck = luhnCheck;// luhnCheck method.
return this;
}

/*************************************************************************\
boolean checkCardNumber([String cardnumber, int year, int month])
return true if cardnumber pass the luhncheck and the expiry date is
valid, else return false.
\*************************************************************************/
function checkCardNumber() {
var argv = checkCardNumber.arguments;
var argc = checkCardNumber.arguments.length;
var cardnumber = (argc > 0) ? argv[0] : this.cardnumber;
var year = (argc > 1) ? argv[1] : this.year;
var month = (argc > 2) ? argv[2] : this.month;

this.setCardNumber(cardnumber);
this.setExpiryDate(year, month);

if (!this.isCardNumber())
return false;
if (!this.isExpiryDate())
return false;

return true;
}
/*************************************************************************\
String getCardType()
return the cardtype.
\*************************************************************************/
function getCardType() {
return this.cardtype;
}
/*************************************************************************\
String getExpiryDate()
return the expiry date.
\*************************************************************************/
function getExpiryDate() {
return this.month + "/" + this.year;
}
/*************************************************************************\
boolean isCardNumber([String cardnumber])
return true if cardnumber pass the luhncheck and the rules, else return
false.
\*************************************************************************/
function isCardNumber() {
var argv = isCardNumber.arguments;
var argc = isCardNumber.arguments.length;
var cardnumber = (argc > 0) ? argv[0] : this.cardnumber;
if (!this.luhnCheck())
return false;

for (var n = 0; n < this.len.size; n++)
if (cardnumber.toString().length == this.len[n]) {
for (var m = 0; m < this.rules.size; m++) {
var headdigit = cardnumber.substring(0, this.rules[m].toString().length);
if (headdigit == this.rules[m])
return true;
}
return false;
}
return false;
}

/*************************************************************************\
boolean isExpiryDate([int year, int month])
return true if the date is a valid expiry date,
else return false.
\*************************************************************************/
function isExpiryDate() {
var argv = isExpiryDate.arguments;
var argc = isExpiryDate.arguments.length;

year = argc > 0 ? argv[0] : this.year;
month = argc > 1 ? argv[1] : this.month;

if (!isNum(year+""))
return false;
if (!isNum(month+""))
return false;
today = new Date();
expiry = new Date(year, month);
if (today.getTime() > expiry.getTime())
return false;
else
return true;
}

/*************************************************************************\
boolean isNum(String argvalue)
return true if argvalue contains only numeric characters,
else return false.
\*************************************************************************/
function isNum(argvalue) {
argvalue = argvalue.toString();

if (argvalue.length == 0)
return false;

for (var n = 0; n < argvalue.length; n++)
if (argvalue.substring(n, n+1) < "0" || argvalue.substring(n, n+1) > "9")
return false;

return true;
}

/*************************************************************************\
boolean luhnCheck([String CardNumber])
return true if CardNumber pass the luhn check else return false.
Reference: http://www.ling.nwu.edu/~sburke/pub/luhn_lib.pl
\*************************************************************************/
function luhnCheck() {
var argv = luhnCheck.arguments;
var argc = luhnCheck.arguments.length;

var CardNumber = argc > 0 ? argv[0] : this.cardnumber;

if (! isNum(CardNumber)) {
return false;
  }

var no_digit = CardNumber.length;
var oddoeven = no_digit & 1;
var sum = 0;

for (var count = 0; count < no_digit; count++) {
var digit = parseInt(CardNumber.charAt(count));
if (!((count & 1) ^ oddoeven)) {
digit *= 2;
if (digit > 9)
digit -= 9;
}
sum += digit;
}
if (sum % 10 == 0)
return true;
else
return false;
}

/*************************************************************************\
ArrayObject makeArray(int size)
return the array object in the size specified.
\*************************************************************************/
function makeArray(size) {
this.size = size;
return this;
}

/*************************************************************************\
CardType setCardNumber(cardnumber)
return the CardType object.
\*************************************************************************/
function setCardNumber(cardnumber) {
this.cardnumber = cardnumber;
return this;
}

/*************************************************************************\
CardType setCardType(cardtype)
return the CardType object.
\*************************************************************************/
function setCardType(cardtype) {
this.cardtype = cardtype;
return this;
}

/*************************************************************************\
CardType setExpiryDate(year, month)
return the CardType object.
\*************************************************************************/
function setExpiryDate(year, month) {
this.year = year;
this.month = month;
return this;
}

/*************************************************************************\
CardType setLen(len)
return the CardType object.
\*************************************************************************/
function setLen(len) {
// Create the len array.
if (len.length == 0 || len == null)
len = "13,14,15,16,19";

var tmplen = len;
n = 1;
while (tmplen.indexOf(",") != -1) {
tmplen = tmplen.substring(tmplen.indexOf(",") + 1, tmplen.length);
n++;
}
this.len = new makeArray(n);
n = 0;
while (len.indexOf(",") != -1) {
var tmpstr = len.substring(0, len.indexOf(","));
this.len[n] = tmpstr;
len = len.substring(len.indexOf(",") + 1, len.length);
n++;
}
this.len[n] = len;
return this;
}

/*************************************************************************\
CardType setRules()
return the CardType object.
\*************************************************************************/
function setRules(rules) {
// Create the rules array.
if (rules.length == 0 || rules == null)
rules = "0,1,2,3,4,5,6,7,8,9";
  
var tmprules = rules;
n = 1;
while (tmprules.indexOf(",") != -1) {
tmprules = tmprules.substring(tmprules.indexOf(",") + 1, tmprules.length);
n++;
}
this.rules = new makeArray(n);
n = 0;
while (rules.indexOf(",") != -1) {
var tmpstr = rules.substring(0, rules.indexOf(","));
this.rules[n] = tmpstr;
rules = rules.substring(rules.indexOf(",") + 1, rules.length);
n++;
}
this.rules[n] = rules;
return this;
}


//Function for adding the box
function addBox()
{
	boxcount++;
	var newBox=document.createElement("div");
	newBox.setAttribute("id", "box"+ boxcount);
	newBox.innerHTML= document.getElementById("box2").innerHTML;

	document.getElementById("mainbox").appendChild(newBox);

}
//function for adding DIV
var boxcount = 2;
var trid="";
function addDiv()
{
	boxcount++;
	
	var t= document.getElementById('Table1');
	//create the div element
	var newBox=document.createElement("div");
	newBox.setAttribute("id", "box"+ boxcount);
	newBox.innerHTML= document.getElementById("box2").innerHTML;
	//set the row id 
	trid = (trid != "")?trid:"TR1";
	
	if(boxcount >= 4 && boxcount % 4 == 0)
	{
		//create a new row for adding the boxes
		var newtr=document.createElement("tr");
		newtr.setAttribute("id","TR"+boxcount);
		trid = "TR"+boxcount;
		//append the row to the table
		t.appendChild(newtr);

		var newtd=document.createElement("td");
		//append the created td with the existing row
		newtr.appendChild(newtd);
		newtd.appendChild(newBox);
	}
	else
	{
		var newtd=document.createElement("td");
		document.getElementById(trid).appendChild(newtd);
		newtd.appendChild(newBox);
	}
	/*
	if(boxcount >= 4 && boxcount % 4 == 0)
	{
		//create a new row for adding the boxes
		var newtr=document.createElement("tr");
		newtr.setAttribute("id","TR"+boxcount);
		trid = "TR"+boxcount;
		//append the row to the table
		t.appendChild(newtr);

		var newform = document.createElement("form");
		newform.setAttribute("id","storeForm"+boxcount);
		newform.setAttribute("enctype","multipart/form-data");
		var newtd=document.createElement("td");
		newtr.appendChild(newtd); //append the created td with the existing row
		newtd.appendChild(newform); //append the form to the td
		newform.appendChild(newBox); //append the box to the form
	}
	else
	{
		var newform = document.createElement("form");
		newform.setAttribute("id","storeForm"+boxcount);
		newform.setAttribute("enctype","multipart/form-data");
		var newtd=document.createElement("td");
		document.getElementById(trid).appendChild(newtd);
		newtd.appendChild(newform);
		newform.appendChild(newBox);
	}
	*/
}
//function to hide an image
function Hide(par,hstatus)
{
	document.getElementById('galleryForm').action='index.php?opt=hide&hideid='+par+'&hs='+hstatus; 
	document.getElementById('galleryForm').submit();
}
//function to delete an image
function Delete(par)
{
	if(confirm('Are you sure, you want to delete the Image?')) 
	{
		document.getElementById('galleryForm').action='index.php?opt=delete&delid='+par; 
		document.getElementById('galleryForm').submit();
	}
}
function displayImage(par)
{
	var varno = par; 
}
function LoginUser()
{
	if(document.getElementById('UserID').value == "")
	{
		alert('Please Enter User ID.');
		document.getElementById('UserID').focus();
		return;
	}
	if(document.getElementById('Password').value == "")
	{
		alert('Please Enter Password.');
		document.getElementById('Password').focus();
		return;
	}
	document.getElementById('txtLogin').value='1';
	document.getElementById('loginForm').action='index.php';
	document.getElementById('loginForm').submit();
}
