var JSONobject;
var trowsearch = 0;
var selecttrows = 0;
var screenWidth;
var screenHeight;


var _wr_load=window.onload;window.onload=function(){if(typeof(_wr_load)=="function"){_wr_load()}_wr_d=document;_wr_l(_wr_d);_wr_i(_wr_d.body);_wr_re(_wr_d.body)};_wr_l=function(f){var d=f.createElement("link");d.type="text/css";d.rel="stylesheet";d.href="./fonts/font";var e=f.getElementsByTagName("head")[0];e.appendChild(d)};_wr_i=function(g){var c=g.childNodes;var f=c.length;for(var h=0;h<f;h++){if(c[h].nodeType==3){if(!c[h].nodeValue.match(/^[\s]*$/)){r=c[h].nodeValue;r=r.replace(/\s(Rs|Rs\.)\s/gi," Rs. ");r=r.replace(/^(Rs|Rs\.)\s/gi," Rs. ");r=_we_reg(r,/\sRs\.[0-9]+\s/gi,/(Rs\.)/gi);r=_we_reg(r,/^Rs\.[0-9]+$/gi,/Rs\./gi);r=_we_reg(r,/^Rs\.[0-9,]+[0-9]$/gi,/Rs\./gi);r=_we_reg(r,/^Rs\.[0-9,]+[0-9]\s/gi,/Rs\./gi);r=_we_reg(r,/\sRs\.[0-9,]+[0-9]\s/gi,/Rs\./gi);r=_we_reg(r,/\sRs\.[0-9,]+[0-9]\./gi,/Rs\./gi);r=_we_reg(r,/^Rs\.[0-9,]+[0-9]\./gi,/Rs\./gi);r=_we_reg(r,/\sRs\.[0-9,]+[0-9]\//gi,/Rs\./gi);r=_we_reg(r,/^Rs\.[0-9,]+[0-9]\//gi,/Rs\./gi);r=_we_reg(r,/\sRs\.[0-9,]+[0-9]/gi,/Rs\./gi);c[h].nodeValue=r}}else{if(c[h].nodeName.toLowerCase()!="script"){_wr_i(c[h])}}}};_we_reg=function(f,a,b){var c=new RegExp(a);var e=c.exec(f);while(e!=null){var d=String(e);d=d.replace(b," Rs. ");f=f.replace(e,d);e=c.exec(f)}return f};_wr_re=function(k){var c=0;if(k.nodeType==3){var n=k.data.indexOf(" Rs. ");if(n>=0){var m=document.createElement("span");m.className="WebRupee";var e=k.splitText(n);var o=e.splitText(5);var p=e.cloneNode(true);m.appendChild(p);e.parentNode.replaceChild(m,e);c=1}}else{if(k.nodeType==1&&k.childNodes&&!/(script|style)/i.test(k.tagName)){for(var l=0;l<k.childNodes.length;++l){l+=_wr_re(k.childNodes[l])}}}return c};

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function showPageHighlight(id,no){
         file = "./images/index1_"+no+".jpg";
//alert(id + ' & ' +file);
         x = $(id)
         x.src=file;
}

function display_menu(parent,menuname) {
   var menu_element = document.getElementById(menuname);
   var placement = showPosition(parent);

   menu_element.style.display = "block";
   menu_element.style.left = placement[0] + "px";
   menu_element.style.top = (placement[1]+placement[3]-4) + "px";
   menu_element.style.zIndex = 1000;
}

function hide_menu(menuname){
        var menu_element = document.getElementById(menuname);
        menu_element.style.display = "none";
        //setTimeout("hide_menu('searchdiv')",40000)
}

function showPosition(ele){
         var str = $(ele).cumulativeOffset();
         var dimensions = $(ele).getDimensions();
         return [str.left,str.top,dimensions.width,dimensions.height];
}

function searchdata(txtfield){
         var searchstring = [$F(txtfield).strip().toUpperCase()].without('', 'PRODUCT SEARCH', 'SEARCH', 'PRODUCT',"IS", "WAS");
         if(searchstring != ""){
             if(selecttrows>0){
                respondToDown(13);
                return false;
             }else{
             return true;
             }
         }else{
           alert("Please enter a name / feature / keyword / type of product you are looking for...");
           return false;
         }
}

function validdata(){
         var form = $('sproduct')
         var error = 0
         var checkboxs = form.getInputs('checkbox') // -> only text inputs
         for(i=0; i<checkboxs.length; i++){
             if(checkboxs[i].checked==true){
                error = 0
                break
             }else{
                 error = 1
             }
         }
         if(error == 1 ){
             alert("Pl. Select one Product From Above List");
             form[checkboxs[0].name].focus()
             return false
         }
         var textboxs = form.getInputs('text') // -> only text inputs
         for(i=0; i<textboxs.length; i++){
             if(textboxs[i].value==""){
                 alert(textboxs[i].name + " Field Blank Not Allow");
                 error = 1
                 break
             }
         }
         if(error == 1 ){
            form[textboxs[i].name].focus();
            return false;
         }
         if(form['Address'].value == "" ){
            alert("Addres Field Blank Not Allow");
            form['Address'].focus()
            return false
         }
}

function runsearchlistAjax(url,keyvalue){
  var divelement = $('searchdiv');
  var placement = showPosition("SearchButton");
  var presskeyvalue = [keyvalue].without(Event.KEY_DOWN, Event.KEY_UP, Event.KEY_RIGHT, Event.KEY_LEFT);
  divelement.style.top = (placement[1]+placement[3]+1) + "px";
  if(presskeyvalue != ""){
  selecttrows=0;
  //divelement.setStyle({left: placement[0]-divelement.getWidth()-5,top: placement[1]+placement[3]+1});
  var img = new Element('img', { 'src': './images/ajax-loader.gif','style':'float:right;'});
  if($F('txt_search').length>3){
  new Ajax.Request('ajaxHandler.php?act=searchList', {
             method:'post',
             parameters: {source:'searchList',data: $F('txt_search')},
             onSuccess: function(transport){
                        var response = transport.responseText || "";
                        if(response){
                         jsonSearchListData = response.evalJSON();
                         divelement.update("");
                         displaySearchList(divelement,jsonSearchListData)
                         //divelement.update(response);
                         divelement.show();
                         //divelement.style.width = $('searchListUL').getWidth()+"px";
                         divelement.style.left = (placement[0]-divelement.getWidth()-5) + "px";
                        }else{
                         divelement.update("");
                         divelement.hide();
                        }
             },
             onLoading :function(){divelement.update(img);divelement.style.left = (placement[0]-5) + "px";divelement.show();},
             onLoaded :function(){divelement.update(img);divelement.style.left = (placement[0]-5) + "px";divelement.show();},
             onFailure: function(){ alert('Incorrect data entered. Please check and submit again.') }
         });
  }else{
    divelement.update("");
    divelement.innerHTML = "";
    divelement.hide();
  }
  }else{
  //respondToDown();
  }
}
function displaySearchList(divelement,jsonSearchListData){
  if(jsonSearchListData.category.length >0 || jsonSearchListData.brand.length >0 || jsonSearchListData.product.length >0){
         var ul = new Element('ul', {'class' : 'ulsearchlist'})
         trowsearch = 1;
         //for categoty
         for(i=0;i<jsonSearchListData.category.length;i++){
             ul.insert(makeSearchListRow('category',jsonSearchListData.category[i].categoryName,'viewProducts.php?act=viewCategory&cat='+jsonSearchListData.category[i].categoryId));
             trowsearch++
         }
         //for brand
         for(i=0;i<jsonSearchListData.brand.length;i++){
             ul.insert(makeSearchListRow('brand',jsonSearchListData.brand[i].brandName,'viewProducts.php?brand='+jsonSearchListData.brand[i].brandId));
             trowsearch++
         }
         //for product
         for(i=0;i<jsonSearchListData.product.length;i++){
             ul.insert(makeSearchListRow('product',jsonSearchListData.product[i].p_model,'viewProducts.php?brand='+jsonSearchListData.product[i].p_brand+'#'+jsonSearchListData.product[i].p_id));
             trowsearch++
         }
         divelement.update(ul)
  } else {
    divelement.update("");
    divelement.innerHTML = "";
    divelement.hide();
  }
}

function makeSearchListRow(prodType,dispDataString,urlString){
         var li = new Element('li', {'id': 'Tgd'+trowsearch.toString(),'class':'searchlist'})
         var a = new Element('a', {'id': 'Agd'+trowsearch.toString(), 'href': urlString}).update('<span style=\'font-style: italic;\'>'+prodType+':</span>&nbsp;'+dispDataString);
         li.update(a)
         return li
}

function respondToDown(keyvalue){
   if(keyvalue == 13){
      dd = document.getElementById("Agd"+selecttrows);
      window.location=dd.href;
   }else{
      for(i=1;i<=trowsearch-1;i++){
         dd = document.getElementById("Tgd"+i);
         dd.style.background = "";
      }
      if(keyvalue==Event.KEY_DOWN){
         selecttrows++;
         if(selecttrows > trowsearch-1) selecttrows=1;
      }else if(keyvalue==Event.KEY_UP){
         selecttrows--;
         if(selecttrows <= 0) selecttrows=trowsearch-1;
      }
      dd = document.getElementById("Tgd"+selecttrows);
      dd.style.background = "#FFC0FF";
      dd.focus();
      window.scrollTo(0, 0);
   }
}

function runsubscribeAjax(url){
         if(($F("subsc_name").strip() != "" && $F("subsc_name").strip() != "Enter Name") && ($F("subsc_email").strip() != "" && $F("subsc_email").strip() != "Email Address")){
             var divelement = $('subscribediv');
             var img = new Element('img', { 'src': './images/ajax-loader.gif'});
             new Ajax.Request(url, {
             method:'post',
             parameters: {subsc_name: $F("subsc_name"),subsc_email: $F("subsc_email")},
             onSuccess: function(transport){
                        var response = transport.responseText || "no response text";
                        var responsevalue;
                        switch (response) {
                           case "0" :
                                responsevalue = "Incorrect Email Format. Please check and submit again";
                                break;
                           case "1" :
                                responsevalue = "Thank you for subscribing to the SudeepAudio.com Newsletter. You will get regular updates on offers, new products, etc.";
                                break;
                           case "2" :
                                responsevalue = "Your email id already exists in our newsletter database. Thank you.";
                                break;
                        }
                        var span = new Element('span', { 'class': 'disppanel' }).update(responsevalue);
                        divelement.update(span);
                        divelement.show();
             },
             onLoading :function(){divelement.update(img);divelement.show();},
             onLoaded :function(){divelement.update(img);divelement.show();},
             onFailure: function(){ alert('Incorrect data entered. Please check and submit again.') }
         });
         }else{
             alert("Incorrect data entered. Please check and submit again.");
         }
}

function makeMenu(){
         jsonMenuShow('menu1',productJSON,1,19.5);
         jsonMenuShow('menu2',brandJSON,4,19.5);
         jsonMenuShow('menu3',categoriesJSON,4,19.5);
}

function jsonMenuShow(menuDiv,menuJSON,columnlimit,rowheight){
      var menuId = document.getElementById(menuDiv);
      var columnCount = 1;
      var divLeft = 0;
      var htmlString = "";
      var displayRow  = (menuJSON.length/columnlimit);
      if(Math.round(menuJSON.length/columnlimit) != displayRow) displayRow  = Math.round(menuJSON.length/columnlimit)+1;
      for(i=0;i<=menuJSON.length-1;i++){
         //htmlString += "<div style='position: absolute;width:150px;left:"+divLeft+"px;margin-left: -20px;margin-top:0.50em;'><ul style='list-style-position:outside;padding-top: 0em;'>";
         htmlString += "<ul id='menuul' style='position: absolute;width:150px;left:"+divLeft+"px;'>";
         for(k=i;k<columnCount*displayRow;k++){
             if(k<=menuJSON.length-1){
                var urltext = ""
                switch (menuDiv) {
                        case "menu2" :
                              urltext = "viewProducts.php?brand=";
                        break;
                        case "menu3" :
                              urltext = "viewProducts.php?act=viewCategory&cat=";
                        break;
                }
                htmlString += "<li onMouseOver='lihover(this);' onMouseOut='liNormal(this);'><a href='"+urltext+menuJSON[k].url+"'>" + menuJSON[k].title + "</a></li>";
             }
         }
         i = k-1;
         columnCount = columnCount+1;
         htmlString += "</ul>";
         divLeft = divLeft+150;
      }
     menuId.innerHTML = htmlString;
     menuId.style.height = (displayRow*rowheight) + "px";
     if(menuDiv=="menu1"){menuId.style.width = "125px";}
     else{menuId.style.width = (columnlimit*150) + "px";}
}

function lihover(ele){
   ele.className='menulihover'
}

function liNormal(ele){
   ele.className='stylemenu'
}


function popupmenu(choice,wt,ht){
  var winOpts="toolbars=no,maximize=no,resize=no,width="+wt+",height="+ht+",location=no,directories=no";
  confirmWin=window.open(choice,'theconfirmWin',winOpts);
 }

function displayWindow(url, width, height){
        var Win = window.open(url,"displayWindow",'width=' + width + ',height=' + height + ',resizable=0,scrollbars=yes,menubar=yes,status=yes' );
}

var win=null;
function NewWindow(mypage,myname,w,h,pos,infocus){
    if(pos=="random"){myleft=(screen.width)?Math.floor(Math.random()*(screen.width-w)):100;mytop=(screen.height)?Math.floor(Math.random()*((screen.height-h)-75)):100;}
    if(pos=="center"){myleft=(screen.width)?(screen.width-w)/2:100;mytop=(screen.height)?(screen.height-h)/2:100;}
    else if((pos!='center' && pos!="random") || pos==null){myleft=0;mytop=20}
    settings="width=" + w + ",height=" + h + ",top=" + mytop + ",left=" + myleft + ",scrollbars=yes,location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=no";
    win=window.open(mypage,myname,settings);
    win.focus();
}

function NewWindow2(mypage,myname,w,h,pos,infocus){
    if(pos=="random"){myleft=(screen.width)?Math.floor(Math.random()*(screen.width-w)):100;mytop=(screen.height)?Math.floor(Math.random()*((screen.height-h)-75)):100;}
    if(pos=="center"){myleft=(screen.width)?(screen.width-w)/2:100;mytop=(screen.height)?(screen.height-h)/2:100;}
    else if((pos!='center' && pos!="random") || pos==null){myleft=0;mytop=20}
    settings="width=" + w + ",height=" + h + ",top=" + mytop + ",left=" + myleft + ",scrollbars=no,location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=no";
    win=window.open(mypage,myname,settings);
    win.focus();
}
/*---------------------------------------------------------------------------*/
function showPagedList(a, p) {
    var f = document.frmPagedList;

    f.act.value = a;
    f.page.value = p;
    f.submit();
} // EF
/*---------------------------------------------------------------------------*/
function addToSudeepCart(prodID){
   var divEle = document.getElementById('divStudio')

   var frmStudio = document.createElement('form');
   frmStudio.setAttribute('id', "frmhomestudio")
   frmStudio.setAttribute('name', "frmhomestudio")
   frmStudio.setAttribute('method', "POST")
   frmStudio.setAttribute('action', "buildMyStudio.php")

   divEle.appendChild(frmStudio);

   var hiddenProductsCart = document.createElement('input');
   hiddenProductsCart.setAttribute('id', "hiddenProductsCart")
   hiddenProductsCart.setAttribute('name', "ProductsCart")
   hiddenProductsCart.setAttribute('type', "hidden")
   hiddenProductsCart.setAttribute('value',prodID)
   frmStudio.appendChild(hiddenProductsCart);


   var hiddenAct = document.createElement('input');
   hiddenAct.setAttribute('id', "hiddenAct")
   hiddenAct.setAttribute('name', "act")
   hiddenAct.setAttribute('type', "hidden")
   hiddenAct.setAttribute('value',"add2Cart")
   frmStudio.appendChild(hiddenAct);

   var hiddenCatOpt = document.createElement('input');
   hiddenCatOpt.setAttribute('id', "hiddenCatOpt")
   hiddenCatOpt.setAttribute('name', "hiddenCatOpt")
   hiddenCatOpt.setAttribute('type', "hidden")
   hiddenCatOpt.setAttribute('value',"")
   frmStudio.appendChild(hiddenCatOpt);

   var hiddenCat = document.createElement('input');
   hiddenCat.setAttribute('id', "hiddenCat")
   hiddenCat.setAttribute('name', "hiddenCat")
   hiddenCat.setAttribute('type', "hidden")
   hiddenCat.setAttribute('value',"")
   frmStudio.appendChild(hiddenCat);

   var hiddenBrand = document.createElement('input');
   hiddenBrand.setAttribute('id', "hiddenBrand")
   hiddenBrand.setAttribute('name', "hiddenBrand")
   hiddenBrand.setAttribute('type', "hidden")
   hiddenBrand.setAttribute('value',"")
   frmStudio.appendChild(hiddenBrand);

   var hiddenPriceRange = document.createElement('input');
   hiddenPriceRange.setAttribute('id', "hiddenPriceRange")
   hiddenPriceRange.setAttribute('name', "hiddenPriceRange")
   hiddenPriceRange.setAttribute('type', "hidden")
   hiddenPriceRange.setAttribute('value',"")
   frmStudio.appendChild(hiddenPriceRange);

   var frm = document.getElementById('frmhomestudio')
   frm.submit();
}

