




        

        
var strBoxesRef = 'compare_checkbox_';
var isPhones = true;
var menuItems = [{items:["Телефоны","Оборудование"],lang:"ru"},{items:["Тэлефоны","Абсталяванне"],lang:"be"},{items:["Phones","Equipment"],lang:"en"}];
function getMenuByLang(lang,num){
  for (var i=0;i<menuItems.length;i++) {
     if (menuItems[i].lang==lang && menuItems[i].items.length>num){
        return menuItems[i].items[num];
     }   
  }
  return;    
}
function replaceBreadcrumbMenu(){
   var lang = location.pathname.replace("/cps","").replace("/","").substring(0,2); 
   var isPhones = false,isEquip = false;
   if (location.pathname.indexOf("/phones")!=-1) isPhones = true;
   if (location.pathname.indexOf("/equipment")!=-1) isEquip = true;
    
   var name = "";
   if(isEquip) {
     name = getMenuByLang(lang,1);
     $("div.breadcrumb a").add("span.printselect").get(2).innerHTML=name;
   }else if (isPhones){
     name = getMenuByLang(lang,0);  
     $("div.breadcrumb a").add("span.printselect").get(2).innerHTML=name;
   }    
}        
function submitPhonesForm(action,obj){
    var phoneParams = document.getElementById('phoneParams');
    var phoneForm = document.getElementById('phonesForm');
    var phoneParamsTargetDiv = document.getElementById('phoneParamsTargetDiv');
    
    if (phoneParams && isPhones) {
        var boxes = phoneParams.getElementsByTagName("input");
        var newbox = new Array();
        var j=0;
        for (var i=0; i<boxes.length; i++){
            if(boxes[i].checked){
                var str = '<input type="checkbox" checked="true" name="p_s_att" value="'+ boxes[i].value+'" />';
                newbox[j]=str;
                j++;    
            }
        }
        phoneParamsTargetDiv.innerHTML = newbox.join('');
    
    } 
    if(action){
        phoneForm.action = action;
        phoneForm.method = "POST";
    } else {
        phoneForm.action=document.getElementById('phonesFormAction').innerHTML;
        phoneForm.method = "POST";
        //phoneForm.method = "GET";
    }
   var endpos = phoneForm.action.lastIndexOf("/"); 
   var old_action = phoneForm.action.substring(endpos+1);   
   var new_action;
   if ( old_action!="phones" && old_action!="equipment" && old_action!="compare"){
     new_action = phoneForm.action.substring(0,endpos+1);  
   }else{
     new_action = phoneForm.action+"/";   
   }   
    
   var newName = "";
   
   if (obj){
    var isPhone = obj.parent().parent().parent().children("H5").children("A").attr("id")=="phone_h";   
    if(obj.attr("name")== "phoneBrand" && action=="" && isPhone){ 
     if(obj.context.selectedIndex!=0){
      newName = obj.children("[selected=true]").attr("id");
     }
      phoneForm.action = new_action+newName;
    }
   }
   newName = "";
   
   if (obj){
    var isEquip = obj.parent().parent().parent().children("H5").children("A").attr("id")=="equip_h"; 
    if(obj.attr("name")== "phoneType" && action=="" && isEquip ){
     if(obj.context.selectedIndex!=0){
      newName = obj.children("[selected=true]").attr("id");
     }
      phoneForm.action = new_action+newName;
    }
   }    
  
    if(!phoneForm.phoneToCompare_1){
        var ptc_1 = document.getElementById('phoneToCompare_1');
        if (ptc_1){
          phoneForm.appendChild(ptc_1);
          phoneForm.appendChild(document.getElementById('phoneToCompare_2'));
        }    
    }
    //phoneForm.method = "GET";
    phoneForm.submit();
}
function phonesPaginationChunkProvider(chunk){
 var phoneForm = document.getElementById('phonesForm'); 
 phoneForm.chunk.value = chunk;
 submitPhonesForm();
}           
function clearSelects(){
try{
    var phoneForm = document.getElementById('phonesForm');
    phoneForm.phoneBrand.selectedIndex = 0;
    phoneForm.phonePrice.selectedIndex = 0;
    phoneForm.phoneType.selectedIndex = 0;
    document.getElementById('phoneSortPrice').value='';
 } catch(e){}
}
function handsetsSwitch(){
    var category = document.getElementById('phonesCategory');
    category.value = "handsets";
    clearSelects();
    isPhones = true;
    submitPhonesForm();
}
function modemsSwitch(){
    var category = document.getElementById('phonesCategory');
    category.value = "modems";
    clearSelects();
    isPhones = false;
    submitPhonesForm();
}
function bestsellersSwitch(){
    var category = document.getElementById('phonesCategory');
    var action = document.getElementById('bestsellers_url');
    if(action){action = action.innerHTML};
    category.value = "bestsellers";
    isPhones = false;
    submitPhonesForm(action);
}
function sortPrice(){
    var sortPrice = document.getElementById('phoneSortPrice');
    if (sortPrice.value === "desc") {
        sortPrice.value = "asc";
    } else {
        sortPrice.value = "desc";
    }
    submitPhonesForm();
}
function enableCompare(){
    var button = document.getElementById('compareSelectedButton');
    if(button){
        button.disabled=null;
        button.onclick=comparePhonesSelected;
        button.style.cursor='pointer';
        button.style.color='#FFFFFF';
    }
  
}
function disableCompare(){
    var button = document.getElementById('compareSelectedButton');
    if(button){
        button.disabled="true";
        button.onclick="return false;";
        button.style.cursor='default';
        button.style.color='#676565';
    }
    
}
// way = true for enabling, false for disabling
function manageBoxes(way, check_1, check_2){
    var boxes = document.getElementsByName('compare_checkbox');
    check_2 = (check_2 && check_2.length>0)?check_2: -1;
    
    for (var i=0; i < boxes.length; i++){
        var id = boxes[i].id.substr(strBoxesRef.length);
        if (id != check_1 && id != check_2){
            if (way) {boxes[i].disabled = null;}
            else boxes[i].disabled = "true";
        }
    }
}
function compareCheckClick(checked_id){
    var box = document.getElementById(checked_id);
    var input_check_1 = document.getElementById('phoneToCompare_1');
    var input_check_2 = document.getElementById('phoneToCompare_2')
    if (box.checked){
        if(input_check_1.value.length==0) {
            input_check_1.value = box.id.substr(strBoxesRef.length); 
            disableCompare();
            return;
        } 
        input_check_2.value = box.id.substr(strBoxesRef.length);
        manageBoxes(false, input_check_1.value, input_check_2.value);
        enableCompare();
        return;
    
    } 
    //UNCHECK ( check_1 or check_2 )  if check_2 unchecked -  check_1 will be unchecked too
    if (input_check_1.value == box.id.substr(strBoxesRef.length)){
        input_check_1.value = input_check_2.value;
    } 
    // ANYWAY check_2 will be unchecked, boxes became enabled
    input_check_2.value = '';
    manageBoxes(true, input_check_1.value); // even if check_1.value is empty 
    disableCompare();
    return;
}    
    
        
function comparePhonesSelected(){
    var input_1 = document.getElementById('phoneToCompare_1');
    var input_2 = document.getElementById('phoneToCompare_2');
    
    if (input_1 && input_1.value.length>0 && input_2 && input_2.value.length>0){
     var action = document.getElementById('phonesCompareForm').action;
 //    action = action + '?phoneToCompare_1=' + input_1.value + '&phoneToCompare_2=' + input_2.value;
     submitPhonesForm(action);
    }
}
function manageCheckBoxes(){
    var input_1 = document.getElementById('phoneToCompare_1');
    var input_2 = document.getElementById('phoneToCompare_2');
    var box, box_2;
    if (input_1 && input_1.value.length>0){
        box = document.getElementById(strBoxesRef+input_1.value);
        if (input_2 && input_2.value.length>0){
            box_2 = document.getElementById(strBoxesRef+input_2.value);
        }
    }
    if ( box   ) {
        box.checked   = "true";
        if ( box_2 ) {
            box_2.checked = "true";
            enableCompare();
            manageBoxes(false, input_1.value, input_2.value);
        }
    }
}
        
function productPhonesPage(){
    disableCompare();
    manageCheckBoxes();
}
function disableAdd(qty_id){
    var button = document.getElementById('add_link_'+qty_id.substring('amount_'.length));
    if(button){
        button.style.color='#676565';
        button.style.cursor='default';
        if (button.onclick && !button.disabled){
            button.oncustomclick=button.onclick;
            button.onclick='return false;';
        }
    button.disabled="true";
    }
}
function enableAdd(qty_id){
    var button = document.getElementById('add_link_'+qty_id.substring('amount_'.length));
    if(button){
        if (button.oncustomclick && button.disabled){
            button.onclick=button.oncustomclick;
        }    
        button.style.cursor='pointer';
        button.style.color='#FFFFFF';
        button.disabled=null;
    }
}
function checkEnabledQTY(input){
    if (!isNumber(input.value)){
     disableAdd(input.id);
     return;
    }
    var amount = parseInt(input.value);
            if (isNaN( amount )) {
                disableAdd(input.id)
                return;
            }
    enableAdd(input.id);
}
function onQTYChange(input){
    this.run = function(){
            checkEnabledQTY(input);
        }
}
function setQTYEvents(){
    var formInputs = document.getElementsByTagName('input');
    for (var i = 0; i < formInputs.length; i++) {
        var theInput = formInputs[i];
        if (theInput.type == 'text' && theInput.id.indexOf('amount')>-1) {
                theInput.onkeyup=new onQTYChange(theInput).run;  
                theInput.onkeyup();
        }
    }    
}
function setPaginationProvider(){
 try {
    if (setProvider) {
         setProvider(phonesPaginationChunkProvider);
    }
 } catch (e){
 }
}
function campaignsSwitch(){
var category = document.getElementById('phonesCategory');
category.value = "campaigns";
clearSelects();
isPhones = false;
submitPhonesForm();
}
addOnloadEvent(productPhonesPage);
addOnloadEvent(setQTYEvents);
addOnloadEvent(setPaginationProvider);
addOnloadEvent(replaceBreadcrumbMenu);

