<!--
/*
Java Script procedures for International School pages
*/

var MAX_ITEMS = 30;
var BRWS_TP = 0;    //browser type: 1: Internet Explorer; 2: Firefox; 9: others;
var nItems = 0;
var cItmIf = new Array();

//=========================================================
// Product Info Class
//=========================================================
function prdinfo(){
  this.sSKU = "";     // SKU
  this.sSty = "";     // Style number
  this.sClr = "";     // Color code
  this.sSiz = "";     // Size

  this.sPrdNam = "";  // Product Name
  this.sClrNam = "";  // Color description
  this.nHemFlg = 0;   // Hemming Flag (0: not available; 1: available)
  this.nHemLgh = 0;   // Hemming Length
  this.nQty = 1;      // Quantity
  this.lPrc = 0;      // Price
  this.sImg = 0;      // Product Image

  this.setprdinfo = setprdinfo;
}

function setprdinfo(sSKU){
  aVal = new Array("","","","");
  this.sSKU = sSKU;
  this.sSty = sSKU.substr(0,6);
  this.sClr = sSKU.substr(6,3);
  this.sSiz = sSKU.substr(9);
  aVal = GetProductInfo(this.sSty);
  this.sPrdNam = aVal[0];
  this.lPrc = aVal[1];
  this.nHemFlg = aVal[2];
  this.sImg = aVal[3];
  this.sClrNam = GetColorName(this.sClr);
}

//=========================================================
// Customer Info Class
//=========================================================
function cstinfo(){
  this.sTlt = "";     // Title
  this.sFnm = "";     // First Name
  this.sMnm = "";     // Middle Name
  this.sLnm = "";     // Last Name
  this.sCnr = "";     // Customer number
  this.sZip = "";     // Zip
  this.sPrf = "";     // Prefecture
  this.sTwn = "";     // Town
  this.sAd1 = "";     // Address 1
  this.sAd2 = "";     // Address 2
  this.sAd3 = "";     // Address 3
  this.sEml = "";     // Email
  this.sPhn = "";     // Phone
  this.sFax = "";     // Fax
  this.sPym = "";     // Payment method
  this.sDlt = "";     // Delivery time
  this.sCmt = "";     // Comment (Special delivery instruction)
  this.readfromkuch = readfromkuch;
}
function readfromkuch(){
  this.sTlt = readCustKuch("TLT");     // Title
  this.sFnm = readCustKuch("FNM");     // First Name
  this.sMnm = readCustKuch("MNM");     // Middle Name
  this.sLnm = readCustKuch("LNM");     // Last Name
  this.sCnr = readCustKuch("CNR");     // Customer number
  this.sZip = readCustKuch("ZIP");     // Zip
  this.sPrf = readCustKuch("PRF");     // Prefecture
  this.sTwn = readCustKuch("TWN");     // Town
  this.sAd1 = readCustKuch("AD1");     // Address 1
  this.sAd2 = readCustKuch("AD2");     // Address 2
  this.sAd3 = readCustKuch("AD3");     // Address 3
  this.sEml = readCustKuch("EML");     // Email
  this.sPhn = readCustKuch("PHN");     // Phone
  this.sFax = readCustKuch("FAX");     // Fax
  this.sPym = readCustKuch("PYM");     // Payment method
  this.sDlt = readCustKuch("DLT");     // Delivery time
  this.sCmt = readCustKuch("CMT");     // Comment (Special delivery instruction)
  
  if(this.sTlt == null){this.sTlt = "Mr.";}
  if(this.sFnm == null){this.sFnm = "";}
  if(this.sMnm == null){this.sMnm = "";}
  if(this.sLnm == null){this.sLnm = "";}
  if(this.sCnr == null){this.sCnr = "";}
  if(this.sZip == null){this.sZip = "";} 
  if(this.sPrf == null){this.sPrf = "Tokyo";} 
  if(this.sTwn == null){this.sTwn = "";}
  if(this.sAd1 == null){this.sAd1 = "";}
  if(this.sAd2 == null){this.sAd2 = "";}
  if(this.sAd3 == null){this.sAd3 = "";}
  if(this.sEml == null){this.sEml = "";}
  if(this.sPhn == null){this.sPhn = "";}
  if(this.sFax == null){this.sFax = "";}
  if(this.sPym == null){this.sPym = "1";}
  if(this.sDlt == null){this.sDlt = "na";}
  if(this.sCmt == null){this.sCmt = "";}
}

//=========================================================
// Get Items from Cookies
//=========================================================
function GetCurrentItems(){
  var vItm;
  nItems = 0;
  var sItm = readKuch(nItems);
  while((sItm) && (nItems<MAX_ITEMS)){
    vItm = sItm.split(",",3)
    cItmIf[nItems] = new prdinfo();
    cItmIf[nItems].setprdinfo(vItm[0]);
	cItmIf[nItems].nQty=vItm[1];
	cItmIf[nItems].nHemLgh=vItm[2];
    nItems++;
    sItm = readKuch(nItems);
  }
}
//=======================================================================
// INLINE BAG
//=======================================================================
function PrepInlineBag(idx){
  var n = 0;
  if(nItems>=MAX_ITEMS){
    document.write("<div class='noJavaScript'>");
    document.write("You have already " + MAX_ITEMS + " items in your shopping bag.<br>To continue shopping place the order or remove some items from your shopping bag.");
    document.write("</div>");}

  document.write("<div class='inlineBag' id='inlineBag'>");
    // Header
    document.write("<div id='ibHeader' class='ibHeader'>");
      document.write("Added to my shopping bag:");
    document.write("</div>");
    // Body
    document.write("<div id='ibBody' class='ibBody'>");
	  document.write("<div id='ibPrdDsc' class='ibPrdDsc'>");
        document.write("Product Name");
      document.write("</div>");
	  document.write("<div id='ibPrdAtr' class='ibPrdAtr'>");
        document.write("<span class='featureText'>Color: </span>");
        document.write("<span class='attributeText' id='ibAtrClr'>Color</span><br>");
        document.write("<span class='featureText'>Size: </span>");
        document.write("<span class='attributeText' id='ibAtrSiz'>XXX</span><br>");
        document.write("<span class='featureText'>Quantity: </span>");
        document.write("<select class='attributeText' id='ibAtrQty' onChange='javascript:UpdateTotalPrice();'>");
        document.write("<option value='1' selected>1</option>");
        for(n=1;n<10;n++){
          document.write("<option value=" + (n+1) + ">" + (n+1) + "</option>");}
        document.write("</select><br>");
      document.write("</div>");

	  document.write("<div id='ibPrdHem' class='ibPrdHem'></div>");

	  document.write("<div id='ibPrdPrc' class='ibPrdAtr'>");
        document.write("<span class='featureText'>Price: </span>");
        document.write("<span class='attributeText' id='ibAtrPrc'>" + FormatNbr("1000",true) + "</span><br>");
      document.write("</div>");
    document.write("</div>");

	document.write("<div id='ibFooter' class='ibFooter'>");
      document.write("<div id='ibFooterInfo' class='ibFooterInfo'>");
        document.write("<div id='shoppingBagTotal' class='shoppingBagTotal'>");
          document.write("<span class='featureText'>Shopping bag total: </span>");
          document.write("<span class='attributeText' id='ibTotPrc'>" + FormatNbr("100000",true) + "</span><br>");
        document.write("</div>");
        document.write("<div id='cancelOrdButton' class='cancelShoppingButton'>");
          document.write("<a href='javascript:AddItem(-1)'><img src='/LandsEndJapan/common/icons/cancel.gif' alt='View Shopping Bag' border='0'></a>");
        document.write("</div>");

      document.write("</div>");
		
      document.write("<div id='ibButtons' class='ibButtons'>");
        document.write("<div id=keepShoppingButton' class='keepShoppingButton'>");
          document.write("<a href='javascript:AddItem(0)'><img src='/LandsEndJapan/common/icons/keep_shopping.gif' alt='Keep Shopping' border='0'></a>");
        document.write("</div>");
        document.write("<div id='viewBagButton' class='keepShoppingButton'>");
          document.write("<a href='javascript:AddItem(1)'><img src='/LandsEndJapan/common/icons/viewBagButton.gif' alt='View Shopping Bag' border='0'></a>");
        document.write("</div>");
      document.write("</div>");
      //document.write("<div id='lpContainer'></div>");
    document.write("</div>");
  document.write("</div>");

}

//=======================================================================
// Add new product
//=======================================================================
function AddToBag(sty,clr,siz){
  //var n;
  //var lTot=0;
  if(document.getElementById("inlineBag").style.visibility == "visible"){return;}
  var sSKU="";
  if(sty!=""){
    if(clr.length != 3){clr = "XXX";}
    if(siz == ""){siz = "NA";}
    sSKU=sty + clr + siz;}
  else{return;}
  cItmIf[nItems]=new prdinfo();
  cItmIf[nItems].setprdinfo(sSKU);

  document.getElementById("ibPrdDsc").innerHTML=cItmIf[nItems].sPrdNam;
  document.getElementById("ibAtrClr").innerHTML=cItmIf[nItems].sClrNam;
  document.getElementById("ibAtrSiz").innerHTML=cItmIf[nItems].sSiz;
  document.getElementById("ibAtrQty").value=1;
  if((Math.abs(SCHOOL_ID)==3)||(Math.abs(SCHOOL_ID)==4)){
    document.getElementById("ibAtrPrc").innerHTML=FormatNbr(cItmIf[nItems].lPrc*1.05,true);}
  else{
    document.getElementById("ibAtrPrc").innerHTML=FormatNbr(cItmIf[nItems].lPrc,true);}
  WriteHemSelection();
  if(cItmIf[nItems].nHemFlg==1){
     document.getElementById("ibPrdHem").style.display="block";}
  else{
     document.getElementById("ibPrdHem").style.display="none";}
  document.getElementById("ibAtrHem").selectedIndex=0;

  UpdateTotalPrice();
  ShowInlineBag();
}
function WriteHemSelection(){
  var sInHtml="<span class='featureText'>Hemming: </span>";
  sInHtml += "<select class='attributeText' id='ibAtrHem'>";
  sInHtml += "<option value='0' id='hmln0' selected>unfinished</option>";
  if(cItmIf[nItems].nHemFlg==1){
    var n;
    var hp = new hempar();
    hp.gethempar(cItmIf[nItems].sSty,cItmIf[nItems].sSiz);
    for(n=hp.nMin;n<(hp.nMax+1);n++){
      sInHtml += "<option value='" + n + "' id='hmln" + n + "'>" + n + "</option>";}}
  sInHtml += "</select>";
  document.getElementById("ibPrdHem").innerHTML=sInHtml;
}
// count total
function UpdateTotalPrice(){
  var n;
  var lTot=0;
  //var lTax=0;
  var nQty=document.getElementById("ibAtrQty").value;
  cItmIf[nItems].nQty=nQty;

  for(n=0;n<(nItems+1);n++){
    //lTax+=Math.floor((cItmIf[n].nQty * cItmIf[n].lPrc)*0.05);
    lTot+=(cItmIf[n].nQty * Math.floor(cItmIf[n].lPrc)*1.05);}
  document.getElementById("ibTotPrc").innerHTML=FormatNbr(lTot,true);
 // + "<br>(" + FormatNbr(lTot+lTax,true) + ")";
}

function AddItem(nShowBag){
  if(nShowBag>-1){
    var nQty=document.getElementById("ibAtrQty").value;
    var nLen=document.getElementById("ibAtrHem").value;
    cItmIf[nItems].nQty = nQty;

    cItmIf[nItems].nHemLgh = nLen;

    writeKuch(nItems);
    nItems++;}
  HideInlineBag();
  if(nShowBag>0){
    switch(Math.abs(SCHOOL_ID)){
	case 1:
      window.location="./bst_shopbag.html";
	  break;
	case 3:
      window.location="/LandsEndJapan/common/html/tyis_shopbag.html";
	  break;
	case 4:
      window.location="/LandsEndJapan/common/html/mnst_shopbag.html";
	  break;
	default:
      window.location="./inscl_shopbag.html";
	  break;}}
}
//=======================================================================
// Toggle inline bag display
//=======================================================================
var nOpct=100;  //Opacity
function ShowInlineBag(){
  var nTop=0;
  CheckBrowserType();
  nTop=GetTop();
  document.getElementById("inlineBag").style.visibility="visible";
  document.getElementById("inlineBag").style.top = (nTop + 150) + "px";
  nOpct = 0;
  ToggleInlineBag(1);
}
function GetTop(){
  var nTop=0;
  if(BRWS_TP == 1){
    nTop=document.documentElement.scrollTop;
    if(nTop==0){nTop=document.body.scrollTop;}}
  else{nTop=pageYOffset;}
  return nTop;
}
function HideInlineBag(){
  nOpct = 100;
  ToggleInlineBag(-1);
}
function ToggleInlineBag(nTp){
  var nStop = 0;
  //CheckBrowserType();
  if(BRWS_TP == 1){
    document.getElementById("inlineBag").filters.alpha.opacity=nOpct;}
  else{
    document.getElementById("inlineBag").style.opacity=(nOpct/100);}
    //document.getElementById("inlineBag").style.-moz-opacity=(nOpct/100);}

  nOpct=nOpct+(10*nTp);
  if (nTp > 0){
    nStop = (nOpct>100) ? 1 : 0;}
  else{
    nStop = (nOpct< 0) ? 1 : 0;}

  if(nStop == 0){
    window.setTimeout("ToggleInlineBag(" + nTp + ")",50);}
  else{
    if(nTp<0){
      document.getElementById("inlineBag").style.visibility="hidden";
      document.getElementById("inlineBag").style.top="-1000px";}
    else{InlineBagVis();}}
}
function ScrollInlineBag(){
  var nTop=0;
  if(document.getElementById("inlineBag").style.visibility == "visible"){
    nTop = GetTop();
    document.getElementById("inlineBag").style.top = (nTop + 150) + "px";}
}
function InlineBagVis(){
  if(document.getElementById("inlineBag").style.visibility == "visible"){
    ScrollInlineBag();
    document.getElementById("shopBagLbl").style.visibility="hidden";
    //document.getElementById("highLevelRow2").style.visibility="hidden";
    document.getElementById("fullPage").disabled=true;
    window.setTimeout("InlineBagVis()",100);}
  else{
    document.getElementById("shopBagLbl").style.visibility="visible";
    //document.getElementById("highLevelRow2").style.visibility="visible";
    document.getElementById("fullPage").disabled=false;}
}


//======================================================================
//=======================================================================
function DeleteAllCookies(){
   var n = 0;
   var vFld = new Array("TLT","FNM","MNM","LNM","CNR","ZIP","PRF","TWN","AD1","AD2","AD3","EML","PHN","FAX","PYM","DLT","CMT");
   for(n=0;n<MAX_ITEMS;n++){delKuch(n);}
   for(n=0;n<vFld.length;n++){delCustKuch(vFld[n]);}
}
//=======================================================================
// Cookie's functions
//=======================================================================
var nExpHrs = 168;    //168 hours (7 days)
function writeKuch(nIdx){
  var sVal = cItmIf[nIdx].sSKU + "," + cItmIf[nIdx].nQty + "," + cItmIf[nIdx].nHemLgh;
  var sNam = Math.abs(SCHOOL_ID) + "prd" + nIdx;
  // var sKuch = sNam + "=" + escape(sVal);
  // ===================================================
  // keep data for a week
  var kuch_date = new Date ( );
  kuch_date.setTime(kuch_date.getTime() + (nExpHrs*60*60*1000));
  var sKuch = sNam + "=" + escape(sVal) + "; expires=" + kuch_date.toGMTString() + "; path=/";
  // ===================================================
  
  document.cookie = sKuch;
}
function readKuch(nIdx){
  var sNam = Math.abs(SCHOOL_ID) + "prd" + nIdx;
  var sRes = document.cookie.match ( '(^|;) ?' + sNam + '=([^;]*)(;|$)' );

  if (sRes){
    return (unescape (sRes[2]));}
  else{
    return null;}
}
function delKuch(nIdx){
  var sNam = Math.abs(SCHOOL_ID) + "prd" + nIdx;
  var kuch_date = new Date ( );
  kuch_date.setTime(kuch_date.getTime() - 1);
  document.cookie = sNam + "=; expires=" + kuch_date.toGMTString() + "; path=/";
}
//=======================================================================
//var nExpCust = 1;    //keep personal data for only an hour
function writeCustKuch(sNam){
  var sKey = Math.abs(SCHOOL_ID) + sNam;
  if ((sNam=="PYM1")||(sNam=="PYM2")){sKey = Math.abs(SCHOOL_ID) + "PYM";}

  var sVal=document.getElementById(sNam).value;
  // ===================================================
  //var kuch_date = new Date ( );
  //kuch_date.setTime(kuch_date.getTime() + (nExpHrs*60*60*1000));
  //var sKuch = sNam + "=" + escape(sVal) + "; expires=" + kuch_date.toGMTString() + "; path=/";
  // ===================================================
  var sKuch = sKey + "=" + escape(sVal) + "; path=/";
  document.cookie = sKuch;
}

function readCustKuch(sNam){
  var sKey = Math.abs(SCHOOL_ID) + sNam;
  var sRes = document.cookie.match ( '(^|;) ?' + sKey + '=([^;]*)(;|$)' );

  if (sRes){
    return (unescape (sRes[2]));}
  else{
    return null;}
}
function delCustKuch(sNam){
  var sKey = Math.abs(SCHOOL_ID) + sNam;
  var kuch_date = new Date ( );
  kuch_date.setTime(kuch_date.getTime() - 1);
  document.cookie = sKey + "=; expires=" + kuch_date.toGMTString() + "; path=/";
}
//============================================================
//============================================================
function ConfKuch(nMode){
  var sKey = "INSCL";
  var sVal=SCHOOL_ID;
  var kuch_date = new Date ( );
  if(nMode==1){
    kuch_date.setTime(kuch_date.getTime() + (nExpHrs*60*60*1000));
    var sKuch = sKey + "=" + escape(sVal) + "; expires=" + kuch_date.toGMTString() + "; path=/";
    document.cookie = sKuch;}
   else{
     sVal=document.cookie.match ( '(^|;) ?' + sKey + '=([^;]*)(;|$)' );
     kuch_date.setTime(kuch_date.getTime() - 1);
     document.cookie = sKey + "=; expires=" + kuch_date.toGMTString() + "; path=/";
     if (sVal){
       return (unescape (sVal[2]));}
     else{
       return 0;}}
}

//=======================================================================

//=======================================================================
// Text formatting & others
//=======================================================================
function FormatNbr(nbr,bPrc){
  var i;
  var sTmp=nbr;
  nbr = '' + nbr;
  if(nbr.length>3){
    var mod = nbr.length % 3;
	var sTmp = (mod>0 ? (nbr.substring(0,mod)) : '');
	for(i=0; i<Math.floor(nbr.length/3);i++){
	  if((mod != 0) ||(i != 0)){sTmp += ","}
	sTmp += nbr.substring((mod+3*i),(mod+3*i)+3);}
	
  }
  if(bPrc){sTmp = "&#165; " + sTmp;}
  return sTmp;
}

//=========================================================
// Check Browser Type
//=========================================================
function GetBrowserType(nTp){
  var n;
  var sOut="";
  var sT="";
  var sTmp="";

  var sURL = (nTp>0) ? readCustKuch("txtURL") : document.getElementById("txtURL").value;
  if(sURL){
    for(n=0;n<sURL.length;n++){
      sOut += sURL.charCodeAt(n);}
	sT=sOut.substr(6,6).split("");
	sTmp=sOut.substr(12,6) + String.fromCharCode(45) + sT.reverse().join("") + String.fromCharCode(45) + sOut.substr(0,6);
	if((sTmp==LEJ_ACNT)&&(nTp==0)){writeCustKuch("txtURL");}
    return (sTmp==LEJ_ACNT) ? true : false;}
  else{
    return false;}
  
}
function CheckBrowserType(){
  if(navigator.appName == "Microsoft Internet Explorer"){BRWS_TP = 1;}
  else if(navigator.appName == "Netscape"){BRWS_TP = 2;}
  else{BRWS_TP = 9;}
}


//-->