function clearBox(objElement){ 
   objElement.value="" 
} 

function changePage(newLoc) {
   nextPage = newLoc.options[newLoc.selectedIndex].value
   if (nextPage != "") {
      document.location.href = nextPage
   }
 }

 /*-------------------------------*/
 //Used to clear keyword search box if not changed
function checkKeyword(objElement) { 
   if (objElement.value == "Keyword/Catalogue Code"){
      objElement.value="" 
   }
} 

function changePage(newLoc) {
   nextPage = newLoc.options[newLoc.selectedIndex].value
   if (nextPage != "") {
      document.location.href = nextPage
   }
 }
 
 function useTandcsPopup() {
    var popup = window.open("http://localhost:8080/ecom_ocr/public/popupContent.jsf?contentPage=tandc.html","popup",config="height=600, width=800, toolbar=no, menubar=no, scrollbars=yes, resizable=yes, location=no, directories=no, status=no"); 
    popup.moveTo(0,0);
 }
 
 function useRelativeTandcsPopup() {
	    var popup = window.open("../public/popupContent.jsf?contentPage=tandc.html","popup",config="height=600, width=800, toolbar=no, menubar=no, scrollbars=yes, resizable=yes, location=no, directories=no, status=no"); 
	    popup.moveTo(0,0);
 }


/*-------------------------------*/
 //Used on the Basket Page
function sub() {
   document.BasketForm.submit();
}

function esub() {
   document.Empty.submit();
}


/*-------------------------------*/
//browser checker
 // <![CDATA[
  var ua= navigator.userAgent;
  var OPERA= (ua.indexOf("Opera") > 0);
  var OMNI= (ua.indexOf("Omni") > 0);
  var MAC= (navigator.platform.indexOf("PPC") > 0);
  var WIN= (navigator.platform=="Win32");
  var IE6, IE5_6, IE5_5, IE5, N4, N5, NS6, NS6_1, MOZ, MAC_IE5;

  if(!OPERA && !OMNI){
     IE6= (ua.indexOf("MSIE 6") > 0);
     // IE 5.5 and IE 5.6 are similar. IE 5.6 is released on WindowsXP
     IE5_6= (ua.indexOf("MSIE 5.6") > 0);
     IE5_5= (ua.indexOf("MSIE 5.5") > 0 || IE5_6);
     // IE5 is true for IE5.5, IE5.6, and IE6.
     IE5= (ua.indexOf("MSIE 5") > 0  || IE6);
    IE4= (ua.indexOf("MSIE 4") > 0);   
     N4= (document.layers);
     NS6= N5= (ua.indexOf("Gecko") > 0);
     NS6_1= (N5 && ua.indexOf("6.1") != -1);
     MOZ= N5 && !(ua.indexOf("Netscape") > 0);
     MAC_IE5= (MAC && IE5);
}
 // ]]>
   //state old browsers 
   var oldbrowser =  N4, IE4;


/*-------------------------------*/
// function to display an error message
function displayText(text){
   if (document.all) {
      document.all['errormessage'].innerHTML = eval(text);
   } else {
      var visual = document.getElementById("errormessage");
      visual.innerHTML = eval(text);  
   }
}

   //error message

   errorText = "<p><br />(! Please fill in all fields marked red below)<br /><br /></p>";



/*-------------------------------*/
//function to validate email address
// Email Validation. Written by PerlScriptsJavaScripts.com
function check_email(e) {
   ok = "1234567890qwertyuiop[]asdfghjklzxcvbnm.@-_QWERTYUIOPASDFGHJKLZXCVBNM";
   for(i=0; i < e.length ;i++){
      if(ok.indexOf(e.charAt(i))<0){ 
         return (false);
      }  
   } 
   if (document.images) {
      re = /(@.*@)|(\.\.)|(^\.)|(^@)|(@$)|(\.$)|(@\.)/;
      re_two = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
      if (!e.match(re) && e.match(re_two)) {
         return (-1);      
      } 
   }
}



/*-------------------------------*/
   //function to validate form elements in the feedback page
function validate_feedback(f)
   {
      var order_ok=true;
      var error_message="error";
      var go_back_to="";
      var feedback_name=f.feedback_name.value;
      var feedback_subject=f.feedback_subject.value;
      var feedback_email=f.feedback_email.value;
      var feedback_message=f.feedback_message.value;
      var message="";
      //name
         if (f.feedback_name.value.length < 1){ 
            if (oldbrowser){message +="Name \n";}
            else{ document.getElementById("na").className="errortext";}
         }
         else { if (document.all || document.getElementByID){ document.getElementById('na').className='text';}}
                
      //subject
         if (f.feedback_subject.value.length < 1){ 
            if (oldbrowser){message +="Subject \n";}
            else { document.getElementById("su").className="errortext";}
         }
         else { if (document.all || document.getElementByID){ document.getElementById('su').className='text';}
         }                 

      //Email
         if (!check_email(f.feedback_email.value)){ 
            if (oldbrowser){message +="Email     (e.g. yourname@yourdomain.com) \n";}
            else { document.getElementById("em").className="errortext";}
         }
         else { if (document.all || document.getElementByID){ document.getElementById('em').className='text';}
         }        

      //Feedback 
         if (f.feedback_message.value.length < 1){
            if (oldbrowser){message +="Feedback ";}
            else { document.getElementById('fb').className='errortext';}
         }  
         else { if (document.all || document.getElementByID){ document.getElementById('fb').className='text';}
         }         

      //alert for old browsers
            if (f.feedback_name.value.length < 1 || f.feedback_subject.value.length < 1 || f.feedback_message.value.length < 1 || (!check_email(f.feedback_email.value))) {         
            if (oldbrowser){error_message="Please fill in: \n \n" +message;}
            go_back_to=f.feedback_name;
            order_ok=false;
         }

      if (! order_ok) {
         if (oldbrowser){alert(error_message);}
         displayText('errorText')
         go_back_to.focus();
      }
      return order_ok;
   }

/*-------------------------------*/
//function to validate postcode
   function validate_postcode()
   {
      var order_ok=true;
      var error_message="error";
      var go_back_to="";
      var pcode=document.customer.cust_postcode.value;
      var country=document.customer.cust_country.value;
      if (country == "GB") {
         var chkfmt="";
         var valid_formats=["ANNAA","ANNNAA","AANNAA","AANNNAA","ANANAA","AANANAA"];
         go_back_to=document.customer.cust_postcode;
         order_ok=false;
         error_message="Invalid Postcode Format";
         for(var i=0;i<pcode.length;i++) {
            if (pcode.charAt(i).match(/[0-9]/)) {
               chkfmt+="N";
            }
            if (pcode.charAt(i).match(/[a-zA-Z]/)) {
               chkfmt+="A";
            }
         }
         for(var i=0;i<valid_formats.length;i++) {
            if (chkfmt == valid_formats[i]) {
               order_ok=true;
            }
         }
      }
      if (! order_ok) {
         alert(error_message);
         go_back_to.focus();
      }
      return order_ok;
   }
   
   
/*-------------------------------*/
//Function to show ssl cert informaition
   function OpenCertdetails()
   {
      thewindow = window.open('https://www.thawte.com/cgi/server/certdetails.exe?code=GBJCBW1', 'anew', config='height=400,width=450,toolbar=no,menubar=no,scrollbars=yes,resizable=no,location=no,directories=no,status=yes');
   }

 
/*-------------------------------*/
//Function to Validate the fields on the ORD/CHECKOUT Pages before returning to FINAL.
   function validate_delivery()
   {
      var order_ok=true;
      var error_message="error";
      var go_back_to="";
      var ctitle=document.OrderForm.ord_del_title.options[0].selected+"yes";     
      var cname=document.OrderForm.ord_del_name.value;
      var house=document.OrderForm.ord_del_house_no.value;
      var addl1=document.OrderForm.ord_del_add_ln1.value;
      var addl4=document.OrderForm.ord_del_add_ln4.value;
      var addl5=document.OrderForm.ord_del_add_ln5.value;
      var pcode=document.OrderForm.ord_del_postcode.value;
      var country=document.OrderForm.ord_del_country.options[document.OrderForm.ord_del_country.selectedIndex].value;
      if (ctitle =="falseyes" || cname !="" || addl1 !="" || addl4 !="" || pcode !="" || house !="") {   
         if (ctitle =="trueyes" || cname=="" || addl1=="" || addl4=="" || pcode=="" || house=="") {         
            error_message="Please ensure Title, Delivery Name, House Number, Street Name, Town and Postcode are completed.";
            go_back_to=document.OrderForm.ord_del_name;
            order_ok=false;
         } else {
            if (country == "GB") {
              s = pcode;
                 var i;
                 var chkpcode = "";        
                 for (i = 0; i < 4; i++) { 
                     var c = s.charAt(i);
                     chkpcode += c;
                 }
                 chkpcode =chkpcode.toUpperCase();      
                 if (chkpcode != "BFPO" && chkpcode != "B.F."){
               var chkfmt="";
               var valid_formats=["ANNAA","ANNNAA","AANNAA","AANNNAA","ANANAA","AANANAA"];
               go_back_to=document.OrderForm.ord_del_postcode;
               order_ok=false;
               error_message="Invalid Postcode Format";
               for(var i=0;i<pcode.length;i++) {
                  if (pcode.charAt(i).match(/[0-9]/)) {
                     chkfmt+="N";
                  }
                  if (pcode.charAt(i).match(/[a-zA-Z]/)) {
                     chkfmt+="A";
                  }
               }
               for(var i=0;i<valid_formats.length;i++) {
                  if (chkfmt == valid_formats[i]) {
                     order_ok=true;
                  }
               }
              }
            }
         }
      }
      if (! order_ok) {
         alert(error_message);
         go_back_to.focus();
      }else{
         document.OrderForm.submit();
      }
      return order_ok;
   }
  

/*-------------------------------*/
//Function to Validate the fields on the MULTI/FINAL and MULTI/CHECKOUT Pages before submitting the order.
function validate_order() {      
      document.OrderForm.mv_click.value="Place Order!";
      var order_ok=true;
      var error_message="";
      var go_back_to="";
      if (document.OrderForm.mv_credit_card_type.value != "CR" && order_ok) {    
         var ccobj=document.OrderForm.mv_credit_card_number;
         var ccnum="";
         go_back_to=ccobj;
         for(var i=0;i<ccobj.value.length;i++) {
            if (ccobj.value.charAt(i).match(/[0-9]/)) {
               ccnum += ccobj.value.charAt(i);
            }
         }
         var chk1=0;
         for(var i=(ccnum.length-1);i>=0;i-=2) {
            chk1+=parseInt(ccnum.charAt(i));
         }
         var chk2=0;
         for(var i=(ccnum.length-2);i>=0;i-=2) {
            var tst=(parseInt(ccnum.charAt(i))*2);
            if (tst<10) {
               chk2+=tst;
            } else {
               chk2+=((tst%10)+1);
            }
         }
         if (ccnum.length<10 || ((chk1+chk2)%10)!=0) {
            error_message="Invalid Credit Card Number";
            order_ok=false;
         }
         var today=new Date();
         if (document.OrderForm.mv_credit_card_start_month) {
            var sm=document.OrderForm.mv_credit_card_start_month.value;
            var sy=document.OrderForm.mv_credit_card_start_year.value;
            if ((sm!="" || sy!="") && order_ok) {
               error_message="Invalid Start Date";
               go_back_to=document.OrderForm.mv_credit_card_start_month;
               var ism=parseInt(sm,10);
               var isy=parseInt(sy,10);
               if (ism<1 || ism>12) {
               error_message="inv";
                  order_ok=false;
               }
               if (isy<70) {
                  isy+=2000;
               } else {
                  isy+=1900;
               }
               sdate=new Date(isy,ism-1,1);
               if (sdate>today) {
                  order_ok=false;
               }
            }
         }
         if (order_ok) {
           var em=document.OrderForm.mv_credit_card_exp_month.value;
            var ey=document.OrderForm.mv_credit_card_exp_year.value;
            go_back_to=document.OrderForm.mv_credit_card_exp_month;
            if (em=="" || ey=="") {
               error_message="Please Enter an Expiry Month and Year for your Card";
               order_ok=false;
            } else {
               error_message="Invalid Expiry Date";
               var iem=parseInt(em,10);
               var iey=parseInt(ey,10);
               if (iem<1 || iem>12) {
                  error_message="Invalid Expiry Date";
                  order_ok=false;         
               }
               if (iey<70) {
                  iey+=2000;
               } else {
                  iey+=1900;
               }
               iem+=1;
               if (iem>12) {
                  iem=1;
                  iey+=1;
               }
               edate=new Date(iey,iem-1,1);
               if (edate.getTime() < today.getTime()) {
                  order_ok=false;
               }
            }
         }        
      }else{      
          if (document.OrderForm.mv_credit_card_type.value== "CR"){              
         s = document.OrderForm.ord_tot.value;              
         var i;
         var chkval = "";
         removedecimal = ".,$£"; 
         for (i = 0; i < s.length; i++) { 
            var c = s.charAt(i);
            if (removedecimal.indexOf(c) == -1) chkval += c;
         }
         chkval = parseInt(chkval,10);
         s = document.OrderForm.out_cred_lim.value;         
         var i;
         var credval = "";
         removedecimal = ".,£$";
         for (i = 0; i < s.length; i++) { 
            var c = s.charAt(i);
            if (removedecimal.indexOf(c) == -1) credval += c;
         }
         credval = parseInt(credval,10);        
         if (chkval > credval){
            error_message = "Unfortunately, the order you have entered means that the credit limit on your account would be exceeded. You will need to make payments to your account before you can place any further orders. If you have questions regarding the balance on your account or recent payments made, please call OUR Customer Services Number found on the Contact Page";            
            go_back_to=document.OrderForm.mv_credit_card_type;
            order_ok=false;
         }
         if (order_ok) {
               if (chkval < 1500){
                  order_ok=false;
                  error_message="Unfortunately your order could not be processed due to OCR £15.00 minimum credit order requirement. Please either search for more items to place to take your order value over the threshold or alternatively please place your order via the OCR Publications contact centre and pay by cheque or credit card.";
               }
            }  
          } 
      }
      //if (order_ok) {         
      //   if (document.OrderForm.ord_media.value==""){                           
      // error_message="Please Tell us where you heard about the Online Ordering System";
      //    order_ok=false;
      //    go_back_to=document.OrderForm.ord_media;
      //   }            
      //}   

      if (order_ok) {
         var d = document.OrderForm.mv_agree_terms;
         if (d.checked == false) {
         error_message = "You need to agree to the Terms & Conditions before continuing.";
         go_back_to=document.OrderForm.mv_agree_terms;
         order_ok=false;
         }
      }

      if (! order_ok) {
         document.OrderForm.mv_click.value='';
         alert(error_message);
         go_back_to.focus();
      } else {
         document.OrderForm.mv_click.disabled=true;
         document.OrderForm.mv_todo.value="submit";
         document.OrderForm.submit();
      }  
      return order_ok;
   }
