﻿ // cart.js
catalogRow=0;  
resultcount=200;
catcount=0;


//document.onload=fnStart();


function fnStart() {
    //add row references 
    //grows=igtbl_getGridById('MakeGrid').Rows;
    ourInterval = setInterval('fnReady()', 500);
}

function fnReady() {
    //if (typeof CartGrid == 'undefined') {
    //    return;  
     //}
    //else {
     //   clearInterval(ourInterval);
     //   fnLoad();
     //}
}



function fnExitEditQty(cell, newValue) {
//   if (fnTrim(newValue)=="") { 
//        
//        cell.setValue(cellQty);
//   }
//   fnTotalCart();
//  
}

function fnLoad() {
   
//   document.getElementById('divMMCS').onclick=function(){fnSearch();};
//   document.getElementById('imgMMCS').onclick=function(){fnSearch();};
//   document.getElementById('divLocator').onclick=function(){fnLocator();};
//   document.getElementById('btnPrint').onclick=function(){fnPrint();};
//   document.getElementById('divClear').onclick=function(){fnClear();};
//    document.getElementById('imgDelete').onclick=function(){fnClear();};
//CartGrid.
    
}

function fnLocator() {
 //var LocatorWindow=window.open("http://www.supercheapauto.com.au/mystore-locator.aspx");
    var LocatorWindow=window.open("http://www.supercheapauto.com.au/store-locator.aspx"); 

}

function fnSearch() {
   frameMMCS=parent.frames[0]; 
   frameResults=parent.frames[1];  
   parent.document.getElementById('fraResults').style.visibility='hidden';
   parent.document.getElementById('fraCart').style.visibility='hidden';
   parent.document.getElementById('fraMMCS').style.visibility='visible';
 
}
function fnEnterEditQty1(cell) {
   cellQty=cell.getValue(); 
   cell.setValue("");
}

function fnExitEditQty1(cell, newValue) {
    if (fnVal(newValue) == 0 && fnTrim(newValue) != '0') {
            alert("Please enter an order quantity (or a zero )  ..."); 
            newValue=cellQty
    }
    cell.setValue(newValue);     
    cartTotal=0;
     for (grdRow=0; grdRow<8; grdRow++){
            if (parent.frames[2].CartGrid.getItem(grdRow).getCell(7).getValue() >'$' ){
               if (grdRow==cell.getItemIndex() ) {
                    qty=fnVal( newValue );
                } else {
                    qty=fnVal( parent.frames[2].CartGrid.getItem(grdRow).getCell(8).getValue() );
               } 
               price=fnVal( parent.frames[2].CartGrid.getItem(grdRow).getCell(7).getValue() ); 
               
               cartTotal=cartTotal + qty*price;
            }
        }
        parent.frames[2].document.getElementById('divSubTotal').innerHTML = '$' + cartTotal.toFixed(2);
        return newValue ;
}

function fnTotalCart() {
    cartTotal=0;
    itemCount=0; 
     for (grdRow=0; grdRow<8; grdRow++){
            if (parent.frames[2].CartGrid.getItem(grdRow).getCell(7).getValue() >'$' ){
                itemCount=itemCount +1;
                cartTotal=cartTotal + fnVal( parent.frames[2].CartGrid.getItem(grdRow).getCell(7).getValue() )* fnVal( parent.frames[2].CartGrid.getItem(grdRow).getCell(8).getValue() );
                parent.frames[2].document.getElementById('CartGrid_i' + grdRow +'_c8').style.background='#FFFF80'; 
            } else {
                if ((grdRow/2) ==parseInt(grdRow/2)) {
                    parent.frames[2].document.getElementById('CartGrid_i' + grdRow +'_c8').style.background='whitesmoke'; 
                } else {
                    parent.frames[2].document.getElementById('CartGrid_i' + grdRow +'_c8').style.background='white'; 
                }
            } 
        }
        parent.frames[2].document.getElementById('divSubTotal').innerHTML = '$' + cartTotal.toFixed(2);
         if (itemCount==1) {
            parent.frames[1].document.getElementById('divItemCount').innerHTML= "(" + itemCount + " item selected)" 
        } else {
            parent.frames[1].document.getElementById('divItemCount').innerHTML= "(" + itemCount + " items selected)" 
        }  
}


function fnClear() {
      for (grdRow=0; grdRow<8; grdRow++){
         for (grdCol=0; grdCol<12; grdCol++){            
               parent.frames[2].CartGrid.getItem(grdRow).getCell(grdCol).setValue(' ') ;               
          }
           if ((grdRow/2) ==parseInt(grdRow/2)) {
                parent.frames[2].document.getElementById('CartGrid_i' + grdRow +'_c8').style.background='whitesmoke'; 
            } else {
                parent.frames[2].document.getElementById('CartGrid_i' + grdRow +'_c8').style.background='white'; 
            }
        }
        fnTotalCart();
        
 }
function fnDeleteLine1(cell) {
      cell.setValue("x");
      currentRow=CartGrid.getSelectedCell().getItemIndex();
     var confrimYN = confirm ('Do you wish to delete this line?');
     if (confrimYN==false) {
         return false;
        
     } 
     // compress rows
     for (grdRow=currentRow; grdRow<10; grdRow++){  
          for (grdCol=0; grdCol<12; grdCol++){
           CartGrid.getItem(grdRow).getCell(grdCol).setValue(CartGrid.getItem(grdRow+1).getCell(grdCol).getValue());
           }
     }
     fnTotalCart();
     return false;
       
 }
 

function fnVal(value) {
   if ( typeof value == 'undefined') {
        return 0;
   }
   if (value == 'undefined') {
        return 0;
   }
    value=value.toString();
    if (value.substring(0,1)=='$') {
        value=value.substring(1,20);
    } 
    v=parseFloat(value);
     if (isNaN(v)) { v=0}
    return v; 
} 

function fnTrim(sString) {
    while (sString.substring(0,1) == ' ') {
        sString = sString.substring(1, sString.length);
    }
    while (sString.substring(sString.length-1, sString.length) == ' ') {
        sString = sString.substring(0,sString.length-1);
    }
    return sString;
} 

function fnPrint() {
    orderLines=0;
        orderQty=0;
         enq="";
                 
         for (grdRow=0; grdRow<9; grdRow++){
            if (fnVal(CartGrid.getItem(grdRow).getCell(8).getValue()) >0 ){
              
                vehicleDescription= "";
                vehicleDescription=vehicleDescription + fnTrim(CartGrid.getItem(grdRow).getCell(0).getValue());
                vehicleDescription=vehicleDescription + ' ' + fnTrim(CartGrid.getItem(grdRow).getCell(1).getValue()); 
                vehicleDescription=vehicleDescription + ' ' + fnTrim(CartGrid.getItem(grdRow).getCell(2).getValue()); 
                partDescription =""; 
                partDescription = partDescription +  fnTrim(CartGrid.getItem(grdRow).getCell(4).getValue());
                partDescription = partDescription + ' ' +   fnTrim(CartGrid.getItem(grdRow).getCell(5).getValue()); 
                partDescription = partDescription + ' ' +   fnTrim(CartGrid.getItem(grdRow).getCell(10).getValue());  
                //Build request string 
               if (orderLines>0) {
                     enq=enq+"~|";
                } 
                
                enq=enq+ vehicleDescription + "~_" ;
                enq=enq+ partDescription + "~_" ;
                enq=enq+ fnTrim(CartGrid.getItem(grdRow).getCell(3).getValue())  + "~_" ;  
                enq=enq+ fnTrim(CartGrid.getItem(grdRow).getCell(11).getValue())  + "~_" ;  
                enq=enq+ fnTrim( CartGrid.getItem(grdRow).getCell(6).getValue())  +  "~_" 
                enq=enq+ fnVal(CartGrid.getItem(grdRow).getCell(7).getValue()) + "~_" ;
                enq=enq+ fnVal(CartGrid.getItem(grdRow).getCell(8).getValue())  ; 
                
                orderLines = orderLines+1;
            }
        }
         
      
        enq= strReplaceAll(enq,"<","(");
         enq= strReplaceAll(enq,">",")");
         //enq= strReplaceAll(enq,">","]");
         enq= escape(enq);
          if (orderLines==0) {
            alert("There are no items with order quantities on the Part List ...")
            return;
         } 
         //var wpbs=window.open("./PrintOrder.aspx"+"?OrderDetails=" + enq, "PrintOrder","height=546,width=795,left=1,top=1,status=yes,toolbar=no,scrollbars=no,menubar=no,location=no");
		 var wpbs=window.open("./PrintOrder.aspx"+"?OrderDetails=" + enq, "PrintOrder","status=yes,toolbar=no,scrollbars=no,menubar=no,location=no");
					       
}



function fnSubmitOrder() {
        orderLines=0;
        orderQty=0;
        
        totalFreight=0;
         enq="";
         enq=enq+"<Enquiry>" ;
         
         for (grdRow=0; grdRow<9; grdRow++){
            if (fnVal(CartGrid.getItem(grdRow).getCell(8).getValue()) >0 ){
                enq=enq+"<Product>" ;
                enq=enq+"<Part>" + fnTrim(CartGrid.getItem(grdRow).getCell(6).getValue())  + "</Part>";
                vehicleDescription= "";
                vehicleDescription=vehicleDescription + fnTrim(CartGrid.getItem(grdRow).getCell(0).getValue());
                vehicleDescription=vehicleDescription + ' ' + fnTrim(CartGrid.getItem(grdRow).getCell(1).getValue()); 
                vehicleDescription=vehicleDescription + ' ' + fnTrim(CartGrid.getItem(grdRow).getCell(2).getValue()); 
                vehicleDescription=strReplaceAll(vehicleDescription,"<","("); 
                vehicleDescription=strReplaceAll(vehicleDescription,">",")");  
                vehicleDescription=strReplaceAll(vehicleDescription,"&","+");   
                partDescription =""; 
                partDescription = partDescription +  fnTrim(CartGrid.getItem(grdRow).getCell(4).getValue());
                partDescription = partDescription + ' ' +   fnTrim(CartGrid.getItem(grdRow).getCell(5).getValue()); 
                partDescription=strReplaceAll(partDescription,"<","("); 
                partDescription=strReplaceAll(partDescription,">",")");  
                partDescription=strReplaceAll(partDescription,"&","+"); 
                code=fnTrim(CartGrid.getItem(grdRow).getCell(11).getValue()) ; 
                brand=fnTrim(CartGrid.getItem(grdRow).getCell(3).getValue()); 
                enq=enq+"<Description>" + partDescription  + " Vehicle:" + vehicleDescription + " Brand: " + brand + " Code: "+ code + "</Description>";
                //enq=enq+"<Vehicle>" + vehicleDescription  + "</Vehicle>";
                //enq=enq+"<Brand>" + fnTrim(CartGrid.getItem(grdRow).getCell(3).getValue())  + "</Brand>";   
                enq=enq+"<Qty>" +fnVal( CartGrid.getItem(grdRow).getCell(8).getValue())  + "</Qty>";
                enq=enq+"<Price>" + fnVal(CartGrid.getItem(grdRow).getCell(7).getValue())  + "</Price>" ;
                //enq=enq+"<Supplier>" + fnTrim(CartGrid.getItem(grdRow).getCell(11).getValue())  + "</Supplier>" ; 
                enq=enq+"</Product>"   ;
                orderLines = orderLines+1;
            }
        }
         enq=enq+"<Handling>" ;
         enq=enq+"<Total>" + calcFreight().toFixed(2) + "</Total>" ;
         enq=enq+"</Handling>" ;  
         
         enq= strReplaceAll(enq,'"'," ");
         enq= strReplaceAll(enq,"[","(");
         enq= strReplaceAll(enq,"]",")");
         enq= strReplaceAll(enq,"  "," ");
        
         
         enq=enq+"</Enquiry>" ;
         enq= strReplaceAll(enq,"&"," ");
         enq= strReplaceAll(enq,"<","[");
         enq= strReplaceAll(enq,">","]");
         //enq= escape(enq);
          enq=fnFilter(enq);
         if (orderLines==0) {
            alert("There are no items with order quantities on the Part List ...")
            return;
         } 
        var confrimYN = confirm ('Do you wish to order these parts now online?');
        if (confrimYN) {
            //orderWindow="http://website-supercheap.staging.emu.com.au/online-store/shopping-cart/load-cart.aspx" + "?Enquiry=" + enq;
            orderWindow="https://www.supercheapauto.com.au/online-store/shopping-cart/load-cart.aspx" + "?Enquiry=" + enq  ;
            var wpbs=parent.window.open(orderWindow ,"_self") ;
         
            return;
        } 
    
}

function calcFreight() {
    supplierA=new Array();
    newsupplierA=new Array();  
    lineCount=0;
    totalSuppliers=0;   
    totalFreight=0;
    for (grdRow=0; grdRow<10; grdRow++){
        if (fnVal(CartGrid.getItem(grdRow).getCell(8).getValue()) >0 ){
            supplierA[lineCount]=CartGrid.getItem(grdRow).getCell(11).getValue();
            lineCount=lineCount+1 
        }
    }
    //filter unique supplier list 
    for (i=0; i<supplierA.length; i++){
       
        //check for new supplier
        supplier=supplierA[i];
        notFound=true;
        for (s=0; s<newsupplierA.length; s++){
            if(supplier == newsupplierA[s]) {
                notFound=false;
                break; 
           }
        }
        if (notFound==true) {
              newsupplierA[totalSuppliers]=supplier;
              totalSuppliers=totalSuppliers+1;
        } 
    }
    return 8.95*totalSuppliers;    
}

function strReplaceAll(str,strFind,strReplace) {
	var returnStr = str;
	var start = returnStr.indexOf(strFind);
	while (start>=0)
	{
		returnStr = returnStr.substring(0,start) + strReplace + returnStr.substring(start+strFind.length,returnStr.length);
		start = returnStr.indexOf(strFind,start+strReplace.length);
	}
	return returnStr;
} 


function fnTrim(sString) {
    while (sString.substring(0,1) == ' ') {
        sString = sString.substring(1, sString.length);
    }
    while (sString.substring(sString.length-1, sString.length) == ' ') {
        sString = sString.substring(0,sString.length-1);
    }
    return sString;
}  

  
function setSelectionRange(input, selectionStart, selectionEnd) {
    if (input.setSelectionRange) {
        input.focus();
        input.setSelectionRange(selectionStart, selectionEnd);
    } else if (input.createTextRange) {
        var range = input.createTextRange();
        range.collapse(true);
        range.moveEnd('character', selectionEnd);
        range.moveStart('character', selectionStart);
        range.select();
    }
}

function fnFilter(oldStr) {
    try {
        newStr="";
        for (c=0;c<oldStr.length; c++){
            ch=oldStr.charAt(c);
            reg = /[A-Za-z0-9./ ,;{}:\-\[\]$]/ ;
            if (reg.test(ch) ) {
                 newStr=newStr + ch;
            }
       } 
       return newStr; 
   } catch(err) {
    return oldStr;
   }
}

