/*

Cookie related function
build 19 Oct 2006

Change the following parameters to adjust freight and insurance details
*/

var postTo = "https://secure1.wangnet.com.au/linabausch/CheckOutssl.php";//url to post form to

var cookieName = "linabausch"; //cookiename
var formName = "linabausch"; //main form name

//the folowing must be all different from each other (with no intersection)
var sep = "|"; //separater of items used in cookie
var fsep ="$"; //separator use in fieldname
var usep = "#"; //separator used for number of units
var clist = "Visa,MasterCard"; //allowed cards must be subset of "Visa,MasterCard,Amex,DinersClub";
//catagories aray defines headig s for cookie entries
var catagories = new Array('Your Details','','Selected Prints','Additional Costs');

function additionalCharges(ctype,x) {
//userfunction to calculate freight,taxes etc
//could be on order weight, total charge pieces and distance of these distance needs to come form elsewhere
//return oncost total
var oc = 0;
weight = orderWeight();
pieces = orderPieces();
price = orderTotal();
if (ctype == 'Freight') {
 
	 var pieces=0;
	  order = rememberOrder().split(sep);
	  for ( i=0 ; i<order.length ; i++ ) {
	    if ( order[i].length>0 )  {
	    ndata=order[i].split(usep);
	      fielddata = ndata[0].split(fsep);
	      p = (fielddata.length>2)?fielddata[2]:0;
	      units = (ndata.length>0)?ndata[1]:0;

	    	  
	      }
	    }
	  pieces=( isNaN(pieces)) ? 0 : pieces ;

	  var multiple = 0;
		var price = 0;
		var remainder = 0;
			if (pieces != 0) {
		
			if (x == 1) { // vic
				if (pieces <= 6) {
					price = 10;
				}
				if (pieces > 6 && pieces <= 12) {
					price = 15;
				}

				if (pieces > 12) {
					multiple = parseInt(pieces / 12);
					remainder = pieces % 12;
					if (remainder == 0){
						price = 15 * multiple;
					}
					
					if (remainder > 0 && remainder <= 6) {
						price = (15 * multiple) + 10;
					}
					if (remainder > 6 && remainder <= 12) {
						price = (15 * multiple) + 15;
					}
				}
			}
			if (x == 2) { //NSW ACT SA
				if (pieces <= 6) {
					price = 16;
				}
				if (pieces > 6 && pieces <= 12) {
					price = 22;
				}
				if (pieces > 12) {
					multiple = parseInt(pieces / 12);
					remainder = pieces % 12;

					if (remainder == 0){
						price = 22 * multiple;
					}

					if (remainder > 0 && remainder <= 6) {
						price = (22 * multiple) + 16;
					}
					if (remainder > 6 && remainder <= 12) {
						price = (22 * multiple) + 22;
					}
				}
			}
			if (x == 3) { //QLD TAS
				if (pieces <= 6) {
					price = 23;
				}
				if (pieces > 6 && pieces <= 12) {
					price = 36;
				}
				if (pieces > 12) {
					multiple = parseInt(pieces / 12);
					remainder = pieces % 12;

					if (remainder == 0){
						price = 36 * multiple;
					}

					if (remainder > 0 && remainder <= 6) {
						price = (36 * multiple) + 23;
					}
					if (remainder > 6 && remainder <= 12) {
						price = (36 * multiple) + 36;
					}
				}
			}
			if (x == 4) { //WA NT
				if (pieces <= 6) {
					price = 30;
				}
				if (pieces > 6 && pieces <= 12) {
					price = 50;
				}
				if (pieces > 12) {
					multiple = parseInt(pieces / 12);
					remainder = pieces % 12;

					if (remainder == 0){
						price = 50 * multiple;
					}

					if (remainder > 0 && remainder <= 6) {
						price = (50 * multiple) + 30;
					}
					if (remainder > 6 && remainder <= 12) {
						price = (50 * multiple) + 50;
					}
				}
			}
			
			} else {
			
			}
		v = price;// x * ((pieces>0)? (parseInt(pieces/12) + 1):0);
	  
	  
	
//	v = x * ((pieces>0)? (parseInt(pieces/12) + 1):0);
} 
if (ctype=='Insurance') {
 v =(x==1)? ((price>0)?price*.05:0):0;
}
fname="Additional Costs$"+ctype+"$0$0$0$1";
adjustOrder(fname,v)

return oc;
}

function validOrder() {
//rules for order return an error message if not valid otherwise return empty string
var err="";
weight = orderWeight();
pieces = orderPieces();
price = orderTotal();

return err;
}

/*
var insurancechargeunit = 1.1;
var insurancedollarisper = 50; 
var insuranceincrement = 0.5;
var freightcost = 5.25;
var freightchargecutout = 250;
*/
var recentBrowser = false 

if ( ( parseInt( navigator.appVersion ) >= 4 && navigator.appVersion.indexOf("MSIE") != -1 ) ||
     ( parseInt( navigator.appVersion ) >= 6 && navigator.appName.indexOf("Netscape") != -1 ) ) {
	   recentBrowser = true
	//opera 7 also true for these parameters
}
if ( ( parseInt(navigator.appVersion) >= 5 && navigator.appName.indexOf("Netscape") != -1 ) ||
     ( parseInt(navigator.appVersion) >= 6 && navigator.appName.indexOf("Netscape") != -1 ) ) {
	   recentBrowser = true
}

function rCookiesOn() {
  var hascookie = 2;
  if (!document.cookie){
    cookie_name = cookieName;
    document.cookie=cookie_name+"=; path=/";
    (!document.cookie)?hascookie = 0:hascookie=1; 
  }
  return hascookie;
}

function CookieWarning() {
  if ( rCookiesOn()==0 ) {
    alert("Sorry, you need to enable 'Cookies' on your browser to use this site\n"+
          "Look in Edit menu:preferences (Macintosh or Netscape),\n"+
          "Tools menu Internet option (Windows Internet Explorer) on your browser")
          
    // var noCookies = window.open("/cookies.html","NoCookies","HEIGHT=600,WIDTH=450,status=0,resizable=1,scrollbars=1,menubar=0");
  }
}

function getCookieValue(cookie_name) {
  var cv="";
  if ( document.cookie ) {
    index = document.cookie.indexOf(cookie_name+"=");
    if (index != -1) {
      namestart = (document.cookie.indexOf( "=" , index ) + 1);
      nameend = document.cookie.indexOf( ";" , index );
      if (nameend == -1)
        nameend = document.cookie.length;
      cv = document.cookie.substring( namestart , nameend );     
    }
  }
  return cv;
}

function eatCookie(cookie_name) {
  if ( document.cookie ) {
    index = document.cookie.indexOf(cookie_name);
    if (index != -1)
      document.cookie = cookie_name+"=; path=/";
  }
}


function rememberOrder() {
  var order = unescape(getCookieValue(cookieName));
  if ( order=="undefined" )
    order = "";
  return order;
}


function storeOrder(order) {
  document.cookie = cookieName + "="+escape(order)+"; path=/"; 
}

function forgetOrder() {

//set all value to 0 

  eatCookie(cookieName);
  history.go(0);
}



function n2z(d) {
return (isNaN(d)|| d==0)?'':n;
}



function orderItem(fieldname,boxorfull) {
//displays order with an input and updates 'orderTotal' field if present
//appends fieldname and number orderred to cookie or updates number ordered, if zero removes from cookie
// displays item title, price (if not zero) inputbox number ordered in input box, and description (if not blank)
fielddata = fieldname.split(fsep);
if (fielddata.length>1) {
cat = fielddata[0];
title = fielddata[1];
units = (fielddata.length>0)?fielddata[2]:0;
price = (fielddata.length>1)?fielddata[3]:0;
display = (boxorfull=='')?'full':boxorfull;
if(display!='box') {

 document.write('<tr>');

  document.write('<td><b>' + title + '</b></td>');
  if (price>0) {
  document.write('<td align=right>$ ' + twodp(price) + '</td>');
  } else {
     document.write('<td></td>');
   }
 document.write('<td>');
 }
   document.write('<input type ="text" size=1 name="' + fieldname + '" value="' + numberOrdered(fieldname) + '" onChange="if(!adjustOrder(this.name,this.value))this.focus();"></td>');

if(display!='box') {

 document.write('</td></tr>');
 }
 }
}






function adjustOrder(updatefield,units) {
//updates or appends #number ordered to fieldname
//updatefield will be catagory$fieldname[$units][$price][$weight][$summary:0:1][#numberordered]
if (units=="") {units=0;}
if(!isposint(units) && units!=0) {
 alert ('Please enter a positive integer or zero');
 return false;
} else {
  newc="";
  order = rememberOrder().split(sep);
  gotit=0;
  
 for ( i=0 ; i<order.length ; i++ ) {
    if ( order[i].length>1 )  {

    ndata=order[i].split(usep);
    fieldname = ndata[0];
    if(fieldname ==updatefield ) {
     if(units>0) {
     newc+=updatefield+usep+units + sep;
     }
     gotit=1;
   }else {
     if(ndata.length>0 && ndata[1]>0) {newc += order[i] + sep;}
   }
   }
   }
   if (gotit==0 && units >0 ) {
    newc+=updatefield+usep+units + sep;
   }
   storeOrder(newc);
   setTotals();
  return true;
  }
}


function setTotals() {
   if(document.getElementById('clubPieces')) {document.getElementById('clubPieces').innerHTML = getPieces(); }
   if(document.getElementById('orderPieces')) {document.getElementById('orderPieces').innerHTML = orderPieces(); }
   if(document.getElementById('orderTotal')) {document.getElementById('orderTotal').innerHTML = "$" + twodp(orderTotal()); }
   if(document.getElementById('orderWeight')) {document.getElementById('orderWeight').innerHTML = orderWeight(); }

}

function orderTotal() {
//will not include summary values if price set to 0 as it should be
  var total=0;
  order = rememberOrder().split(sep);
  for ( i=0 ; i<order.length ; i++ ) {
    if ( order[i].length>0 )  {
    ndata=order[i].split(usep);
      fielddata = ndata[0].split(fsep);
      price = (fielddata.length>3)?fielddata[3]:0;
      units = (ndata.length>1)?ndata[1]:0;
       total += price*units;
     
      }
    }
  total=(isNaN(total))?0:total;
  return total;
}


function orderWeight() {
  var weight=0;
  order = rememberOrder().split(sep);
  for ( i=0 ; i<order.length ; i++ ) {
    if ( order[i].length>0 )  {
    ndata=order[i].split(usep);
      fielddata = ndata[0].split(fsep);
      w = (fielddata.length>4)?fielddata[4]:0;
      units = (ndata.length>0)?ndata[1]:0;
      weight += w*units;
      }
    }
  weight=( isNaN(weight)) ? 0 : weight ;
  return weight;
}


function orderPieces() {
  var pieces=0;
  order = rememberOrder().split(sep);
  for ( i=0 ; i<order.length ; i++ ) {
    if ( order[i].length>0 )  {
    ndata=order[i].split(usep);
      fielddata = ndata[0].split(fsep);
      p = (fielddata.length>2)?fielddata[2]:0;
      units = (ndata.length>0)?ndata[1]:0;
      pieces += p*units;
      }
    }
  pieces=( isNaN(pieces)) ? 0 : pieces ;
  return pieces;
}


function sortOrder() {
if(catagories) {
 cct=0;
 oldcA = rememberOrder(cookieName).split(sep);
 newc = "";
 for (i=0;i<catagories.length;i++) {
   cat = catagories[i];
   for(j=0;j<oldcA.length;j++) {
    dataA = oldcA[j].split(fsep);
    if (dataA[0]==cat) {
     newc += oldcA[j] + sep;
     cct++;
    }
  }
 }
 storeOrder(newc);
}

}



function orderSummary(editornot,order) {
//displays all ordered items with adjustabble numbersordered boxes
//otype= subscription,order or default is both
if(!editornot) {editable = true;} else {editable = (editornot != 'noedit');}
var osum="";
var sum="";
total=0;
pieces=0;
weight=0;
gottotalcat = false;
 if(!order) {
   order = rememberOrder().split(sep);
  } else {
   order = unescape(order).split(sep);
  }
  for(j=0;j<catagories.length;j++) {
  thiscat = catagories[j];
  if (thiscat.toLowerCase()  =='total') {
   gottotalcat=true;
  }
  osum = "";
 for ( i=0 ; i<order.length ; i++ ) {
    if ( order[i].length>0 )  {
    ndata=order[i].split(usep);
    fieldname = ndata[0];
      fielddata = ndata[0].split(fsep);
      cat = fielddata[0];
      if (cat == thiscat) {
      title= fielddata[1];
      issummary=((fielddata.length>5) && fielddata[5]==1);
   if(issummary ) {
   //do not put in additional costing if editable as this may cause errors in calculations
    if(!editable) {
    units = (ndata.length>1)?parseFloat(ndata[1]):0;
    total += units;
    osum+= '<tr><td>'+title+'</td><td colspan=3></td><td>:</td><td align=right>$ ' + twodp(units) + '</td></tr>';;
    }
   
   } else {
      p =(fielddata.length>2)?fielddata[2]*1:0;
      w = (fielddata.length>4)?fielddata[4]*1:0;
      price = (fielddata.length>3)?fielddata[3]*1:0;
      units = (ndata.length>1)?parseFloat(ndata[1]):0;
      ext = price*units;
      total += price*units;
      pieces += p*units;
      weight += w*units;
  
       if(price!=0 ) {
        osum+= '<tr><td>'+title+'</td>';
        if (editable) {
        osum+='<td><input type=text style="width:20px;text-align:right;" name="osum_' + fieldname + '" value="' + units + '" onChange="if(!adjustOrder(\''+fieldname+'\',this.value))this.focus();updateSummary();"></td>';
        } else {
        osum+='<td align=right>'+units+'</td>';
        }
      osum+='<td>@</td><td align=right>$' + twodp(price) +'</td><td>:</td><td align=right>$ ' + twodp(ext) + '</td></tr>';
      } 
      if(price==0 ) {
       osum+= '<tr><td>'+title+'</td>';
      if (editable) {
       osum+='<td><input type=text style="width:20px;text-align:right;" name="osum_' + fieldname + '" value="' + units + '" onChange="if(!adjustOrder(\''+fieldname+'\',this.value))this.focus();updateSummary();"></td></tr>';
       } else {
        osum+='<td align=right>'+units+'</td>';
        }

       }
      }
      }
      }
      }
      if (osum!="") {
       sum += "<tr><td colspan=4><b>"+thiscat+"</b></td></tr>"+osum;
       }
       }
      if(sum!="") {
      if(!gottotalcat) {
    	sum+='<tr><td colspan=6><hr></td></tr>';
       	sum+='<tr><td><b>Total Prints</b></td><td align=right><b><span id=piececount>'+pieces+'</span></b></td>';
          if(total>0) {
            sum+='<td colspan=3></td><td align=right><b>$ <span id=ordertotal>' + twodp(total) + '</span></b></td>';
          }
          sum+='</tr>';
        }
        sum = "<table width='60%'>"+sum+"</table>";
       
    }
     return sum;
}

function updateSummary() {
if(document.getElementById('piececount')) {
 document.getElementById('piececount').innerHTML = orderPieces();
 }
if(document.getElementById('ordertotal')) {
 document.getElementById('ordertotal').innerHTML = twodp(orderTotal());
 }

}
  


function creditCardPayment() {
//cardsAccepted is set elsewhwere
//nameOnCard, cardNumber, span cardType, cvv, expires, paymentAmount



}

function address(atype) {
//type is 'mail','delivery','physical'
//address_type_line1, line2, town State postcode country




}

function cookieToField(cookieField) {
if(document.getElementById(cookieField)) {
document.getElementById(cookieField).value = rememberOrder();
}
//cookieName gets put into cookiename field for submission
}

function numberOrdered(fieldname) {
//number of pieces ordered order_$fieldname$minorder$description$price#numberordered
//fieldname order_$fieldname$minorder$description$price
  n="";
  order = rememberOrder().split(sep);
  for ( i=0 ; i<order.length ; i++ ) {
    if ( order[i].length>0 )  {
      fielddata = order[i].split(usep);
      if (fielddata[0]==fieldname)  {
      n = parseInt((fielddata.length>0)?fielddata[1]:0);
      }
    }
  }
  //n=( isNaN(n) ? 0 : n );
  return n;
}




function CouldBeEmail(email) {
  //no spaces, an @, a dot somewhere after the @
  if (email.indexOf(" ") != -1) return false;
  var at = email.indexOf("@");
  if (at < 1) return false;
  var lastdot = email.lastIndexOf(".");
  if (lastdot < at) return false;
  return true;
 }

/*
order display

*/


function printSubmit() {
var test = "submitMe(this);";
	
document.write('<form method=post action="'+postTo+'">');
//document.write(getCardInterface());
if (getPieces()== 0 && orderPieces()==0) {
} else {
document.write('<input type=button onclick="'+test+'" value="Proceed to Checkout">'); }
document.write('<input type=hidden name="order" id="order">');
document.write('</form>');
}
function submitMe(formctrl) {
//admit use of memebr_email and membership_number with 0 order

var err = "";
//var theemail = document.forms[formName].member_email.value;
//err =  validOrder();

if (err!="") {
   alert(err);
} else {
      url = postTo;
        sortOrder();
        formctrl.form['order'].value = getCookieValue(cookieName);
        formctrl.form.action = postTo; 
        formctrl.form.submit();
     }
}


function twodp(n) {
  //if (is.js < 1.1) 
  n = n*1 +  .005;
  ns = " "+n;
  ns = ns.substring(1,ns.length);
  var dp = ns.indexOf(".");
  if (dp < 0)
    ns = ns+".00";
  else if (dp == ns.length-2) {
    ns = ns+"0";
    //alert(ns);
    }
  else if (dp < ns.length-1)
    ns = ns.substring(0,dp+3);
  return ns;
}

  function isposint(num) {
    if ( (num == "") || (num <=0) ) {
        return false;
    }
    for (var i=0; i < num.length; i++) {
       var ch= num.substring (i, i+1)
       if (ch < "0" || ch > "9" ) {
       	            return false;
       }
    }
    return true;
}
 

 function setCardType(cn) {
if(cn=='') {
document.getElementById('cardType').innerHTML = 'Credit';
} else {

ct = verifyCardType(cn,clist);
 if (ct=='') {
   alert ( 'Invalid card number entered. Enter a correct card number or empty the field to move on.');
   document.getElementById('cardType').innerHTML = 'Credit';
   document.getElementById('cardNumber').focus();
 } else {
     //cvvd=(ct=='Amex')?'inline':'none'; need to display cvvd for all card types for NAB
   document.getElementById('cardNumber').value = FormatCard(cn);
   document.getElementById('cardType').innerHTML = ct;
   //document.getElementById('cvvdisplay').style.display=cvvd;
 }
}
}

function setExpiry(ed) {
if (ed.toLowerCase()=='mm/yy'||ed=='') {
  document.getElementById('cardExpires').value = 'mm/yy';
  return true;
  } else {
 if(CheckExpiry(ed)) {
    document.getElementById('cardExpires').value = FormatExpiry(ed);
    return true;
 } else {
 alert ('Please enter card expiry as mm/yy or empty the field to move on');
     document.getElementById('cardExpires').focus();
     return false;
 }
}
}


function validateCreditCardPayment() {
err = '';
 ccname = document.getElementById('nameOnCard').value;
 ccnum = document.getElementById('cardNumber').value;
cvv =document.getElementById('cvv').value;
ccexp = document.getElementById('cardExpires').value;
 ccamt = document.getElementById('creditCardPayment').value;
 
 if (ccamt =='' || parseFloat(ccamt) == 0) {
 return true;
   err += 'A positive amount must be entered\n';
 }
 tfee = (document.getElementById('totalfee'))?document.getElementById('totalfee').value:"";
 narrative = (document.getElementById('creditCardNarrative'))?xchange(document.getElementById('creditCardNarrative').value,';',','):"Card Payment";
 if (ccname == '') {
  err += 'No Name on Credit Card was entered\n';
 }
 if (ccnum==''){
  err += 'No credit card number was entered\n';
 } else {
 cctype = verifyCardType(ccnum,clist);
 if (cctype == '') {
	err += 'Only '+clist+' credit cards are accepted\n';
 }
 if (cctype=='Amex' && cvv=='') {
  	err += 'the 4 digit cvv number is required for Amex.\n';

 }
 }
if (ccexp.toLowerCase()=='mm/yy'||ccexp=='') {
   err +='No expiry date was entered';
  } else {
 if(!CheckExpiry(ccexp)) {
  err +='an invalid expiry date was entered, it must be of the form mm/yy\n';
  }
  }
 if (ccamt==''&& tfee!='' ) {
  if (confirm('No amount has been entered but a booking fee of '+tfee+' has been calculated.\nWould you like to pay this amount?')) {
   document.getElementById('creditCardPayment').value = tfee;
   ccamt = tfee;
  } 
  }
 if(ccamt==''||ccamt==0) {
   err +='No payment amount specified\n';
 }
  if(ccamt<0) {
   err +='Negative credit card payments cannot be processed\n';
 }

 if (err!='') {
   alert ('Payment could not proceed\n'+err);
   document.getElementById('cardPaymentValid').value='';
   return false;
 } else {
   document.getElementById('cardPaymentValid').value=ccamt+';'+ccnum+';'+cctype+';'+ccexp+';'+cvv+';'+ccname+';'+narrative;
   return true;
 }
}

function getCardInterface() {
//expects clist to contain allowed cards
ci = '<TABLE>';
ci += '<TR><TD Align=Right><font size="2"><b>Your name as on Card</TD><TD><INPUT TYPE="text" id = "nameOnCard" Name="nameOnCard" id="nameOnCard" value="" size=40>&nbsp;&nbsp;</TD></TR>';
ci += '<TR><TD Align=Right><font size="2"><b><span id="cardType">Credit</span> Card Number</b></TD><TD><INPUT TYPE="Text"  NAME="cardNumber" ID="cardNumber" value="" size=19 maxlength="19" onBlur="setCardType(this.value,clist);"><input type="hidden" name="cardType" value="undefined">';
ci += '</td></tr><tr><td><b>Security Number</b></td><td><input type=text style="width:60px;" id="cvv" name="cvv" value=""></span>&nbsp;';
ci += '&nbsp;&nbsp;<font size="2"><b> Expires <INPUT TYPE="text" ID ="cardExpires" NAME="cardExpires"   value="mm/yy" size=5 maxlength="5" onBlur="setExpiry(this.value);">';
ci += '</TD></TR>';
ci += '</TABLE>';
return ci;
}


// replace quotes et al for hidden fields in faxform dq =" Dequote"
function dq(x) {
   x = xchange(x,"\n",":");
   x = xchange(x,"\"","'");
   return x;
}

function xchange(data,oldS,newS) {
 if ((oldS.length == 0) || (data.length==0)) return data;
  for (var i=0; i<data.length; i++) {
   if (data.substring(i,i+oldS.length) == oldS) {
    data = data.substring(0,i)+newS+data.substring(i+oldS.length,data.length)
    }
   }
  return data
}


function validate(theform) {
if (runtimepass == 1) {
runtimepass=0;
return false;
}
    //client, catupdate, reference
    
	with (theform) {
	var CclientFirstname = clientFirstname.value 
    var CclientLastname  = clientLastname.value;
	var CclientPhone 	= Phone.value;
	
	var CClient = "";
	if (salutation.selectedIndex > 0) CClient = salutation.options[salutation.selectedIndex].text + " ";
	client.value = CClient + CclientFirstname +" " + CclientLastname;

	//CClient = client.value;
	
	var CStreet = Street.value;
	var CSuburb = Suburb.value;
    var CState = State.value;
    var CReference = Reference.value;
    if (CReference == "Use for comments or reference") CReference = "";
    reference.value = CReference;
    
    var CCountry = Country.value;
  //   if ((CCountry=="USA") || (CCountry=="U.S.A.") || (CCountry=="United States") || (CCountry=="United States of America")) {
  //  alert("Sorry we cannot ship to the United States, please contact us for further information");
  //  return false;
  //  }
    var CPostCode = PostCode.value; 
    var CPhone = Phone.value;
    
    var CEmail = ClientEmail.value;
    //var sender = "sales@interpet.biz"
    if (!CouldBeEmail(CEmail)) {
    alert ("Please supply a valid email address");
    return false;
    } else {
    email.value=CEmail;
    }
    
   
    if(PayBy.value=="PayPal")  {cardNumber.value = ""; cardExpires.value=""; nameOnCard.value="";}
   
//    var CCardNumber   = cardNumber.value;
//    var CNameOnCard  = nameOnCard.value;
//    var CExpires = cardExpires.value;
    var CCardNumber   = '';
    var CNameOnCard  = '';
    var CExpires = '';
    var CserverMonth = serverMonth.value * 1;
    var CserverYear = serverYear.value * 1;
//    CType = isCardType(CCardNumber);
//    cardType.value = CType;
    
    /*
      var catstr = "Do not notify when there are product updates";
    if (catupdate.checked) catstr ="Notify when there are product updates";
    catalog.value = catstr;
	*/
    
    if ((CclientPhone=="") || (CclientLastname=="") || (CclientFirstname == "") ||  (CStreet == "") || (CSuburb == "") || (CPostCode == "") ) {
    alert("Please supply Name, daytime phone number, full address including postcode and your email address");
    return false;
    }
} //with
 	return true;
}

 
 
function trim (data) {
   var cn = "";
   var c="";
    var L = data.length
   if (L > 0) {
     for (var i=0; i< L;i++) {
        c = data.substring(i,i+1);
     if ((c >="0") && (c <= "9")) cn += data.substring(i,i+1)
    }
   }
   return  cn;
 }
    
    
 function FormatCard (cnum) {
   cnum = trim(cnum);
   var  fcnum ="";
  for (var i = 4; i<= 16; i +=4)  fcnum += cnum.substring(i-4,i)+" ";
  return fcnum;
 }
 
 function FormatExpiry (edate) {
   edate = trim(edate)
   if (edate.substring(0,1) > 1) edate = "0"+edate;
   edate = edate.substring(0,2) + "/" + edate.substring(edate.length - 2,edate.length);
   return edate;
   }


function verifyCardType(cn,clist) {
//just check first two letters against lis vis,mas,ame, din
 ct = isCardType(cn);
 rv = "";
 clistA = clist.split(",");
 for(c=0;c<clistA.length;c++) {
   if(ct.substring(0,3).toLowerCase()==clistA[c].substring(0,3).toLowerCase()) {
      rv = ct;
     break;
   }
 }
 return rv;
}



function isCreditCard(cn) {
  var sum = 0; 
  var mul = 1; 
  var tproduct = 0;
  var l = cn.length;
  for (i = l-1; i >= 0; i--) {
     var digit = cn.substring(i,i+1);
      tproduct = parseInt(digit ,10)*mul;
     if (tproduct >= 10)
      sum += (tproduct % 10) + 1;
    else
      sum += tproduct;
    if (mul == 1)
      mul++;
    else
      mul--;
  }
   if ((sum % 10) == 0)
   {
    return (true);
    }
  else {
    return (false);
   }
}


function isVisa(cc)
{
  if (((cc.length == 16) || (cc.length == 13)) &&
      (cc.substring(0,1) == 4))
    return true;
  return false;
}  // END FUNCTION isVisa()


function isMasterCard(cc)
{
  firstdig = cc.substring(0,1);
  seconddig = cc.substring(1,2)*1;
  if ((cc.length == 16) && (firstdig == 5) &&
      ((seconddig >= 1) && (seconddig <= 5))) { 
     return true;
    }
  return false;

} // END FUNCTION isMasterCard()

function isAmericanExpress(cc)
{
  firstdig = cc.substring(0,1);
  seconddig = cc.substring(1,2);
  if ((cc.length == 15) && (firstdig == 3) &&
      ((seconddig == 4) || (seconddig == 7)))
    return true;
  return false;

} // END FUNCTION isAmericanExpress()

function isDinersClub(cc)
{
  firstdig = cc.substring(0,1);
  seconddig = cc.substring(1,2);
  if ((cc.length == 14) && (firstdig == 3) &&
      ((seconddig == 0) || (seconddig == 6) || (seconddig == 8)))
    return true;
  return false;
}

function isBankCard(cc)
{
  firstdig = cc.substring(0,1);
  seconddig = cc.substring(1,2);
  if ((cc.length == 16) && (firstdig == 5) && (seconddig == 6))
    return true;
  return false;
}



function isCardType (cardNumber)
{
   var cc=trim(cardNumber);
    if (!isCreditCard(cc)) return "invalid";
    if (isVisa(cc)) return "Visa";
    if (isMasterCard(cc)) return "MasterCard";
    if (isBankCard(cc)) return "BankCard";
    if (isAmericanExpress(cc)) return "Amex";
    if (isDinersClub(cc)) return "DinersClub";
     return "unknown";
}

function isValidExipry(data,thisyr,thismth) {
// of the form mm(CHAR)yy or mmyy
// check using server time

var m=0;

if (data.length < 3) return false;

var m = data.substring(0,1);
if (!isposint(m) && m !=0) return false;
if (m > 1)  data = "0"+data;
m = data.substring(0,2);
if (!isposint(m) || (m < 1) || (m >12)) return false;

var y = data.substring(data.length - 2,data.length)
if ((y.length != 2) && (y.length != 4) && (!isposint(y))) return false; 

expy = 2000 + y*1;
if ((expy < thisyr) || ((expy == thisyr) && (m*1 <= thismth)) ) return false;
return true;
}


function CheckExpiry(data) {
var m=0;

td = new Date();
thisyr = td.getFullYear();
thismth = td.getMonth();

if (data.length < 3) return false;

var m = data.substring(0,1);
if (!isposint(m) && m !=0) return false;
if (m > 1)  data = "0"+data;
m = data.substring(0,2);
//if (!isposint(m) || (m < 1) || (m >12)) alert ("Expiry Date: " + data + " is incorrect. Please check it and confirm it is entered thus: mm/yy");


var y = data.substring(data.length - 2,data.length)
//if ((y.length != 2) && (y.length != 4) && (!isposint(y))) alert ("Expiry Date: " + data + " is incorrect, Please use the date on your card. Please check it and confirm it is entered thus: mm/yy");


expy = 2000 + y*1;
//if ((expy < thisyr) || ((expy == thisyr) && (m*1 <= thismth))) alert ("Expiry Date: " + data + " is in the past, Please use the date on your card. Please check it and confirm it is entered thus: mm/yy");

if ((expy - thisyr > 10) )  {
runtimepass = 1;
alert ("Expiry Date: " + data + " is in excess of 10 years from now, Please check it and confirm it is entered thus: mm/yy");
return false;
}
return true;
}

runtimepass =0;

ct=0;
total = 0;		
		

function getPieces () {
	  var pieces=0;
	  order = rememberOrder().split(sep);
	  for ( i=0 ; i<order.length ; i++ ) {
	    if ( order[i].length>0 )  {
	    ndata=order[i].split(usep);
	      fielddata = ndata[0].split(fsep);
	      p = (fielddata.length>2)?fielddata[2]:0;
	      units = (ndata.length>0)?ndata[1]:0;
    	}
	   }
	  pieces=( isNaN(pieces)) ? 0 : pieces ;
	  return pieces;
}

function getNormalPieces () {
	  var pieces=0;
	  order = rememberOrder().split(sep);
	  for ( i=0 ; i<order.length ; i++ ) {
	    if ( order[i].length>0 )  {
	    ndata=order[i].split(usep);
	      fielddata = ndata[0].split(fsep);
	      p = (fielddata.length>2)?fielddata[2]:0;
	      units = (ndata.length>0)?ndata[1]:0;
  	}
	   }
	  pieces=( isNaN(pieces)) ? 0 : pieces ;
	  return pieces;
}

function checkClub() {
	var check = 0;
	order = rememberOrder().split(sep);
	for ( i=0 ; i<order.length ; i++ ) {
	    if ( order[i].length>0 )  {
	    	ndata=order[i].split(usep);
	    	fielddata = ndata[0].split(fsep);
	    	p = (fielddata.length>2)?fielddata[2]:0;
	    	units = (ndata.length>0)?ndata[1]:0;
	    }
	}
	if (check==1) {
		return true;
	} else {
		return false;
	}
}