//--------------------------------------------------------------------------------------------------------- Your Custom JS Functions Here -------
function check()
{
    var j;
    for(j=1;j<9;j++)
    {
         var i=0;
         var a="ctl00_cpSite_dl"+j+"_ctl0"+i+"_txt";
         var b= "ctl00_cpSite_dl"+j+"_ctl0"+i+"_lbln";
         var c = "ctl00_cpSite_dl"+j+"_ctl0"+i+"_lblp";
         while(document.getElementById(a))
        {
               var count=document.getElementById(a).value.replace(/^\s+|\s+$/g,"");
               var name=document.getElementById(b).innerHTML;
               var price=document.getElementById(c).innerHTML;
               if(count!="")
               {
                     cart.addData("",name,price*count,count);
               }
               i++;
               if(i<10)
              {
                   a="ctl00_cpSite_dl"+j+"_ctl0"+i+"_txt";
                   b= "ctl00_cpSite_dl"+j+"_ctl0"+i+"_lbln";
                   c = "ctl00_cpSite_dl"+j+"_ctl0"+i+"_lblp";
              }
               else
              {
                    a="ctl00_cpSite_dl"+j+"_ctl"+i+"_txt";
                    b= "ctl00_cpSite_dl"+j+"_ctl"+i+"_lbln";
                    c= "ctl00_cpSite_dl"+j+"_ctl"+i+"_lblp";
              }
          }
     }
}

function add()
{
    cart.addData("","Number Card","8.50","1");
}


function   ForDight(Dight,How)  
{  
            Dight   =   Math.round   (Dight*Math.pow(10,How))/Math.pow(10,How);  
            return   Dight;  
} 

/** 
Cookie class 
*/ 
function Cookie(){ 
/** 
@desc set Cookie 
@return void 
*/ 
this.setCookie=function(name, value, hours){ 
var expire = ""; 
if(hours != null){ 
expire = new Date((new Date()).getTime() + hours * 3600000); 
expire = "; expires=" + expire.toGMTString(); 
} 
document.cookie = escape(name) + "=" + escape(value) + expire; 
} 
 
/** 
@desc get Cookie 
@return String 
*/ 
this.getCookie=function(name){ 
var cookieValue = ""; 
var search = escape(name) + "="; 
if(document.cookie.length > 0){  
offset = document.cookie.indexOf(search); 
if (offset != -1){  
offset += search.length; 
end = document.cookie.indexOf(";", offset); 
if (end == -1) end = document.cookie.length; 
cookieValue = unescape(document.cookie.substring(offset, end)) 
} 
} 
return cookieValue; 
} 

} 

function Car(name){ 
this.carName = name; 
this.expire = 24*30;//(30days) 
this.carDatas = new Array();
this.numbers = true; 
this.total =0;
this.cookie = new Cookie(); 
this.typeObj=function(name,value){
this.name =name; 
this.value=value; 
} 
this.proObj=function(name,price,amount){
this.name =name; 
this.price=price;
this.amount=amount; 
} 
 

//##private function########################################################## 
// 
//getTypePoint(typeName);
//getProPoint(typeName,proName);
//saveCookie()
// 
//######################################################################## 
 
/** 
@desc 
@return int 
*/ 
this.getTypePoint=function(typeName){ 
var isok=false; 
var i=0; 
for(;i<this.carDatas.length;i++){ 
if(this.carDatas[i].name==typeName){ 
isok=true;
break; 
} 
} 
if(isok)return i; 
else return -1; 
} 
 
/** 
@desc 
@return int 
*/ 
this.getProPoint=function(typeId,proName){ 
var isok=false; 
var j = 0; 
var tempProObj=this.carDatas[typeId].value; 
for(;j<tempProObj.length;j++){ 
if(tempProObj[j].name==proName){ 
isok=true; 
break; 
} 
} 
if(isok)return j; 
else return -1; 
} 

this.replacementOfDangerousSymbols = function(str){
var pattern = /\:/ig;
str = str.toString().replace(pattern, "__COLON__");
pattern = /\|/ig;
str = str.toString().replace(pattern, "__VLINE__");
pattern = /\#/ig;
str =str.toString().replace(pattern, "__POUND__");
pattern = /\,/ig;
str = str.toString().replace(pattern, "__COMMA__");
return str;
}

this.restoreSymbols=function(str){
var pattern = /\_\_COLON\_\_/ig;
str = str.toString().replace(pattern, ":");
pattern = /\_\_VLINE\_\_/ig;
str = str.toString().replace(pattern, "|");
pattern = /\_\_POUND\_\_/ig;
str = str.toString().replace(pattern, "#");
pattern = /\_\_COMMA\_\_/ig;
str = str.toString().replace(pattern, ",");
return str;
}
 
/** 
@desc cookie
@return void 
*/ 
this.saveCookie=function(){ 
var outStr=''; 
for( i=0; i<this.carDatas.length; i++ ){ 
var typeName =this.carDatas[i].name; 
typeName = this.replacementOfDangerousSymbols(typeName);
var typeValue=this.carDatas[i].value; 
var proOutStr=''; 
for( j=0; j<typeValue.length; j++ )
{
var proName = this.replacementOfDangerousSymbols(typeValue[j].name) ;
var proPrice = this.replacementOfDangerousSymbols(typeValue[j].price);
var proAmount = this.replacementOfDangerousSymbols(typeValue[j].amount);
if ( j==0 )proOutStr = proName + ':' + proPrice + ':' + proAmount; 
else proOutStr += '|' + proName + ':' + proPrice + ':' + proAmount; 
} 
if ( i==0 )outStr = typeName + '#' + proOutStr; 
else outStr += ',' + typeName + '#' + proOutStr; 
} 
this.cookie.setCookie(this.carName,outStr,this.expire);
} 
 
if(this.cookie.getCookie(name)==''){ 
this.cookie.setCookie(name,'',this.expire); 
}else{ 
var cookie = this.cookie.getCookie(name);
var tempTypes=cookie.split(','); 
for(i=0;i<tempTypes.length;i++){ 
var tempTypeObj=tempTypes[i].split('#'); 
var type_pro=new Array(); 
if(tempTypeObj[1]){ 
var tempProObj=tempTypeObj[1].split('|'); 
for(j=0;j<tempProObj.length;j++){ 
var proDesc=tempProObj[j].split(':'); 
type_pro.push(new this.proObj(this.restoreSymbols(proDesc[0]),this.restoreSymbols(proDesc[1]),this.restoreSymbols(proDesc[2]))); 
} 
} 
this.carDatas.push(new this.typeObj(this.restoreSymbols(tempTypeObj[0]),type_pro)); 
} 
} 
//##public funtion######################################################### 
// 
//addType(typeName);//add a sprot
//addPro(typeName,proName,price,amount);//add a product
//editPro(typeName,proName,price,amount);//update product
//delPro(typeName,proName);//delete a product
//delType(typeName);//delete a sport
//delCar();//clear cart data
// 
//getCar();//get cart data
//getType();//get all sprots
//getPro(typeName);//get all products
//getProPrice(typeName,proName);//get product price
//    getProAmount(typeName,proName);       //get product amount 
//
//######################################################################## 
 
/** 
@desc 
@return bool 
*/ 
this.addType=function(typeName){ 
if(this.getTypePoint(typeName)!=-1)return false;
this.carDatas.push(new this.typeObj(typeName,new Array()));
this.saveCookie();
return true; 
} 
 
/** 
@desc add product to cart
@return bool 
*/ 
this.addPro=function(typeName,proName,price,amount){ 
var typePoint=this.getTypePoint(typeName);if ( typePoint ==-1 ){this.addType(typeName); typePoint=this.getTypePoint(typeName);}
var proPoint =this.getProPoint(typePoint,proName);
if ( proPoint != -1 ) 
{
if(this.numbers)
{
this.carDatas[typePoint].value[proPoint].price = parseInt(this.carDatas[typePoint].value[proPoint].price) + parseInt(price);
this.carDatas[typePoint].value[proPoint].amount = parseInt(this.carDatas[typePoint].value[proPoint].amount) + parseInt(amount);
}
else
return false;
}
else
this.carDatas[typePoint].value.push(new this.proObj(proName,price,amount));
this.saveCookie();
return true; 
} 
 
/** 
@desc update product.
@return bool 
*/ 
this.editPro=function(typeName,proName,price,amount){ 
var typePoint=this.getTypePoint(typeName);if ( typePoint == -1 ) return false;
var proPoint =this.getProPoint(typePoint,proName);if ( proPoint == -1 ) return false;
if(amount >= 1)
    return this.editProIndex(typePoint, proPoint, price, amount);
else
    return this.delProIndex(typePoint, proPoint);
} 

this.editProIndex=function(typePoint, proPoint, price, amount){
this.carDatas[typePoint].value[proPoint].price=price;
this.carDatas[typePoint].value[proPoint].amount=amount;
this.saveCookie();
return true; 
}

 
/** 
@desc delete product.
@return bool 
*/ 
this.delPro=function(typeName,proName){ 
var typePoint=this.getTypePoint(typeName);if ( typePoint == -1 ) return false;
var proPoint =this.getProPoint(typePoint,proName);if ( proPoint == -1 ) return false;
return this.delProIndex(typePoint, proPoint);
} 

this.delProIndex=function(typePoint, proPoint){
var pros=this.carDatas[typePoint].value.length; 
this.carDatas[typePoint].value[proPoint] = this.carDatas[typePoint].value[pros-1];
this.carDatas[typePoint].value.pop(); 
this.saveCookie();
return true; 
}
 
/** 
@desc delete sports
@return bool 
*/ 
this.delType=function(typeName){ 
var typePoint=this.getTypePoint(typeName);if ( typePoint == -1 ) return false;
return this.delTypeIndex(typePoint);
} 

this.delTypeIndex=function(typePoint){
var types=this.carDatas.length; 
this.carDatas[typePoint] = this.carDatas[types-1];
this.carDatas.pop(); 
this.saveCookie();
return true; 
}
 
/** 
@desc clear cart
@return void 
*/ 
this.delCar=function(){ 
this.cookie.setCookie(this.carName,'',0); 
this.carDatas=new Array(); 
this.saveCookie();
}
 

/** 
@desc get cart data
@return Array 
*/ 
this.getCar=function(){ 
return this.carDatas; 
} 
 
/** 
@desc get sprot list
@return Array 
*/ 
this.getType=function(){ 
var returnarr=new Array(); 
for ( i=0; i<this.carDatas.length; i++)returnarr.push(this.carDatas[i].name); 
return returnarr; 
} 
 
/** 
@desc get product list
@return Array 
*/ 
this.getPro=function(typeName){ 
var typePoint=this.getTypePoint(typeName);if ( typePoint == -1 ) return false;
return this.carDatas[typePoint].value; 
} 
 
/** 
@desc get product price
@return String 
*/ 
this.getProPrice=function(typeName,proName){ 
var typePoint=this.getTypePoint(typeName);if ( typePoint == -1 ) return false;
var proPoint =this.getProPoint(typePoint,proName);if ( proPoint == -1 ) return false;
return this.carDatas[typePoint].value[proPoint].price; 
} 

/** 
@desc get product amount
@return String 
*/ 
this.getProAmount=function(typeName,proName){
var typePoint=this.getTypePoint(typeName);if ( typePoint == -1 ) return false;
var proPoint =this.getProPoint(typePoint,proName);if ( proPoint == -1 ) return false;
return this.carDatas[typePoint].value[proPoint].amount; 
}


this.showCart=function(obj, btnID,tax){
  var total= 0;
  var html = "<table cellpadding='5' cellspacing='0' width='100%' border='0'>";
  html += "<tr style='background:#310000;color:#fff;'><td width='40%'>Item Name</td><td>Qty</td><td>Total</td><td>Delete</td></tr>";
  for( i=0; i<this.carDatas.length; i++ ){ 
  var typeValue=this.carDatas[i].value; 
  for( j=0; j<typeValue.length; j++ )
  {
    html += "<tr>";
    html += "<td><div style='margin-right:150px'>" + typeValue[j].name + "</div></td>";
    html += "<td><input type='text' id='amount_" + i + "|" + j + "' value='" + typeValue[j].amount + "' size='3'>&nbsp;&nbsp;<input onclick='updatePro(this, \"" + obj + "\",\""+tax+"\")' type='button' id='update_" + i + "|" + j + "|" + typeValue[j].amount + "|" + typeValue[j].price + "' value='Update' class='buttonNormal'></td>";
    html += "<td style='color:#fe2221'>$" + typeValue[j].price + "</td>";
    html += "<td><input type='button' id='delete_" + i + "|" + j + "' value='Delete' onclick='deletePro(this, \"" + obj + "\",\""+tax+"\")' class='buttonNormal'></td>"
    html += "</tr>";
    html += "<tr><td colspan='4'><div style='border-top:1px DASHED #cccccc;height:1px'>&nbsp;</div></td></tr>";
    total += parseFloat(typeValue[j].price);
  } 
  }
  html += "</table>";
  var t=parseFloat(total)*(1+parseFloat(tax));
  document.getElementById(obj).innerHTML =html+ "<div  style='float:left;font-weight:bold;font-size:14px;padding:5px 5px;'>Tax: <font color='#fe2221'>"+ tax*100+"%</font></div><div style='float:left;font-weight:bold;font-size:14px;padding:5px 20px;'>Your Shopping Cart: <font color='#fe2221'>$" + ForDight(t,2) + "</font></div><div class='clear'></div>" ;
  this.total = total;
  if(total == 0)
    document.getElementById(btnID).style.display = "none";
}

this.addData=function(typeName, name, price,count){
    this.addPro(typeName, name.replace(/\_\_QUOT\_\_/ig, "'"), price, count);
}

this.redirct = function(){
  //window.location="/cartlist.aspx";
}
}

var cart= new Car("BAS");

function updatePro(obj, containerID,tax){
    var proInfo = obj.id.split('_')[1].split('|');
    var typePoint = proInfo[0];
    var proPoint = proInfo[1];
    var oldAmount = proInfo[2];
    var price = proInfo[3];
    var newAmount = document.getElementById("amount_" + typePoint+ "|" + proPoint).value;
    if(newAmount == "")
    {
        alert("Quantity can not be blank.");
        document.getElementById("amount_" + typePoint+ "|" + proPoint).focus();
        return false;
    }
    if(isNaN(newAmount))
    {
       alert("Quantity must be number.");
       document.getElementById("amount_" + typePoint+ "|" + proPoint).select();
       return;
    }
    if(parseInt(oldAmount) == newAmount)return;
    if(newAmount == "" || newAmount < 1)cart.delProIndex(typePoint, proPoint);
    else cart.editProIndex(typePoint, proPoint, price/oldAmount*newAmount, newAmount);
    cart.showCart(containerID,"ctl00_cpSite_btnSubmit",tax);
}

function isdigit(s)
{
    var r,re;
    re = /\d*/i;
    r = s.match(re);
    return (r==s)?1:0;
}


function deletePro(obj, containerID,tax){
    var bln = confirm('Are you sure you want to delete this record.');  
    if(bln == false) return;    
    var proInfo = obj.id.split('_')[1].split('|');
    var typePoint = proInfo[0];
    var proPoint = proInfo[1];
    cart.delProIndex(typePoint, proPoint);
    cart.showCart(containerID,"ctl00_cpSite_btnSubmit",tax);
}































