// Various Scripts pertaining to the pages.

var onlyNums = new RegExp(/^\d{10}$/); // allow only numbers 

function checkOnlyNums(formname) {
var iqty = document[formname].qty.value;
// var iqty =  = document.getElementById(formname).value;
if((isNaN(iqty)) || (iqty == "0") || (iqty == " ") ||(iqty.length < 1)){
alert("You must add a Quantity only in numbers");
return false;
} else {
return true;
}
}

function getArtist(aitem)
{
aitemId = aitem.name;
ainame = aitem.options[aitem.selectedIndex].text;
document.location.href = 'index.php?searchID=' + ainame;
}

// this is redundant
// need to use only one in shared.js

function emailcheck(strng) {
             str = strng;
             errString = jsString02;
		 at = "@";
		 dot = ".";
		 lat = str.indexOf(at);
	       lstr = str.length;
	       ldot = str.indexOf(dot);
            if(lstr > 3) {
            dummyval = "yes";
            } else {
		   alert(errString);
		   return false;
		}            
		if (str.indexOf(at)==-1){
		   alert(errString);
		   return false;
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert(errString);
		   return false;
		}
		
		 if (str.indexOf(" ")!=-1){
		    alert(errString);
		    return false;
		 }

 		 return true;					
}




// this is redundant
// its the setFormAction function in admin.js
// need to use only one in shared.js
function setFormAction(fname, ffldname, ffldval, sumbmitbool, alertstring)
{
document[fname][ffldname].value = ffldval;
if(alertstring.length > 1){
if((confirm(alertstring)) && (sumbmitbool)) {
document[fname].submit();
} else {
alertstring = "";
}
return;
}
if(sumbmitbool) {
document[fname].submit();
}
}


function getCurr(aitem)
{
aitemId = aitem.name;
ainame = aitem.options[aitem.selectedIndex].value;
document.location.href = 'lang.php?setCurrCode=' + ainame;
}

// product invite to friend pop-up
function invite(theurl)
{
window.open(theurl, 'newWin', 'scrollbars=no,status=no,width=250,height=520,left=230,top=35');
}

// help window pop-up
function gethelp()
{
window.open("help/login.php", 'newWin', 'scrollbars=no,status=no,width=400,height=480,left=230,top=35');
}

// category list pop up
var newwindow = '';
function catList(catId, searchType)
{
var catIdvars = "";
if(searchType == "cstmSearch") {
catIdvars = "searchID=" + catId;
} else {
catIdvars = "categoryID=" + catId;
}
window.open("tplates/catItemList.php?" + catIdvars,'name','scrollbars=yes,status=no,height=480,width=210');
}


function showProd(prodID)
{
  opener.location.href = "../index.php?productID=" + prodID;
}


// to delete, page is included in index.php
function getupdated()
{
window.open ("tplates/mailingsignup.php", 'newWin', 'scrollbars=no,status=no,width=350,height=480,left = 180,top = 25');
}


// to delete, page is included in index.php
function getusrpass()
{
window.open ("tplates/usrforgotusrpass.php", 'newWin', 'status=no,width=350,height=300,left = 180,top = 25');
}

// cart script used in cart.php to update item quantity on select
function UpdateQty(item, ioption)
{
itemId = item.name;
newQty = item.options[item.selectedIndex].text;
document.location.href = 'cart.php?action=update_item&productID=' +itemId+ '&qty=' +newQty+ '&productOptions=' +ioption;
}

function UpdateInQty(item, ioption)
{
newQty = document.getElementById(item).value;
newQty = newQty.replace(/^\s+/g, '').replace(/\s+$/g, '');
if((isNaN(newQty)) || (newQty == "0") || (newQty == " ") || (newQty.length < 1)){
alert("You must add a Quantity only in numbers. Or use the Remove link to remove the item.");
return;
}
document.location.href = 'cart.php?action=update_item&productID=' +item+ '&qty=' +newQty+ '&productOptions=' +ioption;
}


// cart script used in cart.php to update item option on select
function UpdateOption(ioption, itemsId, oldoption, oldqty)
{
itemId = itemsId;
newquantity = oldqty;
newoption = ioption.options[ioption.selectedIndex].value; 				
document.location.href = 'cart.php?action=update_option&productID='+itemId+'&productOptions='+newoption+'&prevoption='+oldoption+'&qty='+newquantity;
}
var submitcount=0;

function checkFields() {
   if (submitcount == 0)
      {
      submitcount++;
      return true;
      }
   else 
      {
      alert(jsString07);
      return false;
      }
}   

function setauthstate() {
    var selectedstate = document.orderform.state_id.options[document.orderform.state_id.selectedIndex].value;
    document.orderform.state.value = selectedstate;
    // document.orderform.state.disabled = true;
}

function checkorderinfo() {
    var selectedstate = document.orderform.state_id.options[document.orderform.state_id.selectedIndex].value;
    var selectedcountry = document.orderform.country.options[document.orderform.country.selectedIndex].value;

    var string0 = document.orderform.firstname.value;
    var string1 = document.orderform.lastname.value;
    var string2 = document.orderform.street.value;
    var string3 = document.orderform.zipcode.value;
    var string4 = document.orderform.city.value;
    var string5 = document.orderform.state.value;
    var string6 = document.orderform.email.value;
    var string7 = document.orderform.tel.value; 
    var string8 = document.orderform.celtel.value;
    var string9 = document.orderform.username.value;
    var string10 = document.orderform.password.value; 
    var string11 = document.orderform.emailconfirm.value;
    var string12 = document.orderform.companyname.value;
    var string13 = document.orderform.vatnum.value;
    var string14 = document.orderform.website.value;
    var string15 = document.orderform.passwordconfirm.value;
    var illegalChars = /[\W_]/; // allow only letters and numbers

var goodEmail = document.orderform.email.value;
if(emailcheck(goodEmail)){
dummyval = "yes";
} else {
dummyval = "no";
return;
}

/*   
    if (!document.orderform.ttall) {
      alert('Your cart is empty.');
      return;
  }  
      var ttaltrim = document.MYFORM.ttall.value
      var ttaltrim = ttaltrim.replace(" €", "");
      var newttrim = ttaltrim;                                
    if (newttrim <= 11.43){
      alert('Sorry, minimum order is 7.23 This does not include shipping fees');
      return;
  } 

*/  

    if ((string0.length <= 1) || (string1.length <= 1)){
      alert(jsString01);
      return;
  } 
      if (string2.length <= 1) {
      alert(jsString03);
      document.orderform.street.value = "00na";
      return;
  }
      if (string3.length <= 1) {
      alert(jsString04);
      return;
  }   
      if ((string4.length <= 1) || (string4.length <= 1)){
      alert(jsString05);
      return;
  }
      if ((string7.length <= 1) && (string8.length <= 1)){
      alert(jsString06);
      document.orderform.tel.value = "00na";
      location.href="#persdata";
      return;
  }
      if (string6 != string11){
      alert(jsString09);
      return;
  }
      if (string15 != string10){
      alert('Passwords do not match.');
      return;
  }

    if ((string9.length < 4) || (string9.length > 12) || (string9 == "")) {
      alert(jsString10);
      location.href="#persdata";
      return;
    }

    if(illegalChars.test(string9)) {
      alert(jsString11);
      return;
    }

    if ((string10.length < 4) || (string10.length > 12) || (string10 == "")) {
      alert(jsString12);
      return;
    }

    if(illegalChars.test(string10)) {
      alert(jsString13);
      return;
    }


    if(document.orderform.usertype.value == "wsaleID"){
    if(string12.length < 3) {
      alert(jsString14);
      return;
    }
    if(string13.length < 1) {
      alert(jsString15);
      return;
    }
    }

    if(selectedcountry == "noselect") {
    alert(jsString17);
    return;
    }    

    if((selectedcountry == "United States") && (selectedstate == "noQvalue")) {
    document.orderform.state.value = "";
    document.orderform.state.disabled = false;
    // document.orderform.state_id[document.orderform.state_id.selectedIndex].text = "USA RESIDENTS";
    // document.orderform.state_id[document.orderform.state_id.selectedIndex].value = "noselect";

    alert("Please select your State from the list");
    return;
    }

    if((selectedcountry != "United States") && (selectedstate != "noQvalue")) {
    document.orderform.state.value = "";
    document.orderform.state.disabled = false;
    // document.orderform.state_id[document.orderform.state_id.selectedIndex].text = "USA RESIDENTS";
    // document.orderform.state_id[document.orderform.state_id.selectedIndex].value = "noselect";
    alert("You have selected a USA State. But your country selection is not United States");
    return;
    }

    if(document.orderform.usertype.value == "affltID"){
    if(string14.length < 1) {
      alert(jsString16);
      return;
    }
    }
document.orderform.state.disabled = false;
document.orderform.submit();
}




// checks the simple signup form
function checkSSignup() {
    var selectedcountry = document.orderform.country.options[document.orderform.country.selectedIndex].value;

    var string0 = document.orderform.firstname.value;
    var string6 = document.orderform.email.value;
    var string9 = document.orderform.username.value;
    var string10 = document.orderform.password.value; 
    var string11 = document.orderform.emailconfirm.value;
    var string15 = document.orderform.passwordconfirm.value;
    var illegalChars = /[\W_]/; // allow only letters and numbers
  
    var goodEmail = document.orderform.email.value;
    if(emailcheck(goodEmail)){
    dummyval = "yes";
    } else {
    dummyval = "no";
    return;
    }
 
    if (string6 != string11){
    alert(jsString09);
    return;
    }

    if (string0.length <= 1){
      alert(jsString01);
      return;
    } 


    if(illegalChars.test(string9)) {
      alert(jsString11);
      return;
    }


    if ((string9.length < 4) || (string9.length > 24) || (string9 == "")) {
      alert(jsString10);
      location.href="#persdata";
      return;
    }



    if(illegalChars.test(string10)) {
      alert(jsString13);
      return;
    }


    if ((string10.length < 4) || (string10.length > 24) || (string10 == "")) {
      alert(jsString12);
      return;
    }

      if (string15 != string10){
      alert('Passwords do not match.');
      return;
    }

    if(selectedcountry == "noselect") {
    alert(jsString17);
    return;
    }    


document.orderform.submit();

}

function printorderinfo(){
text=document.orderinfo.S1.value;
print(text);
}

function getSearch(){
text = document.getElementById("searchID").value;
location.href = "index.php?searchID=" + text;
}

function handleEnter(field, event) {
  var keyCode = event.keyCode ? event.keyCode : 
                event.which ? event.which : event.charCode;
  if (keyCode == 13) {
    getSearch();
    return false;
  }
  else
    return true;
}


function uauthcfrm(thecfrm) {
var string9 = document.uauthcfrmForm.NewCnfStringField.value; 
var string10 = document.uauthcfrmForm.CnfNewCnfStringField.value;
var illegalChars = /[\W_]/; // allow only letters and numbers
/*
if((thecfrm == "chgp") || (thecfrm == "chgu")) {
var goodEmail = document.uauthcfrmForm.email.value;
} else {
var goodEmail = document.uauthcfrmForm.NewCnfStringField.value;
}
if(emailcheck(goodEmail)){
dummyval = "yes";
} else {
dummyval = "no";
return;
}
*/
unmstring = "";
var nthecfrm = thecfrm;
if(nthecfrm == "chgu") {
unmstring = jsString10;
} 

if(nthecfrm == "chgp") {
unmstring = jsString12;
} 

if (string9 != string10){
alert(jsString09);
return;
}
/*
if((nthecfrm != "chgp") && (nthecfrm != "chgu")){
if(emailcheck(goodEmail)){
dummyval = "yes";
} else {
dummyval = "no";
return;
}
}
*/
if((nthecfrm == "chgp") || (nthecfrm == "chgu")){

if ((string9.length < 4) || (string9.length > 12) || (string9 == "")) {
      alert(unmstring);
      return;
}
if ((string10.length < 4) || (string10.length > 12) || (string10 == "")) {
      alert(unmstring);
      return;
}
if((illegalChars.test(string9)) || (illegalChars.test(string10))) {
      alert(unmstring);
      return;
}

}

document.uauthcfrmForm.submit();
}


// new mailing functions

function updtMailingStngs(theAction, theConfString) {
var goodEmail = document.maillistform.usrEmailID.value;

if(emailcheck(goodEmail)){
setFormAction('maillistform', 'mlistAction', theAction, true, theConfString);
}
}
