function setCpuPrice(){
    if(document.getElementById('count_cpu').selectedIndex==1){
        if(active_cpu==mb_intel_id)active_cpu_id=cpu_intel_id;
        else active_cpu_id=cpu_amd_id;
        cpu_elem=document.cfgForm.elements['cat'+active_cpu_id];
        price=eval(cpu_elem.options[cpu_elem.selectedIndex].value);
        if(price)cpu_price=eval(price);    
        else cpu_price=0;
        cpu_tmp_=cpu_elem.options[cpu_elem.selectedIndex].value;
    }
}
function check_dual_cpu(){
    reg_2cpu=/2xxeon/i;
    obj=document.cfgForm.elements['cat'+active_cpu];
    var mb_active_cpu=obj.options[obj.selectedIndex].text;    
    if(mb_active_cpu.match(reg_2cpu)){        
        document.getElementById('count_cpu').disabled=false;
    }
    else {
        document.getElementById('count_cpu').selectedIndex=0;
        document.getElementById('count_cpu').disabled=true;
    }
    setCpuPrice();
}
function calc_total(category_name,product_id){
    check_dual_cpu();
   var total=0; 
    if(category_name && category_name.length>0)fill_price(category_name,product_id);
    check_mb();
    check_cpu();
    check_vga();    
    prices=document.cfgForm.elements['price'];
    for(i=0;i<prices.length;i++){
        total+=prices[i].value*1;
    }
    if (document.getElementById("count_cpu").options.selectedIndex==1)
        total+=cpu_price*1;
    if(total>0)document.cfgForm.total.value=Math.round(total*100)/100;
    else document.cfgForm.total.value="";
    
    // SHOW/HIDE LEASING BUTTON 
    total_dds=document.cfgForm.total.value*1.2;
    total_bgl=total*fixing;
    
    if (total_bgl <= 100){ // HIDE LEASING BUTTON AND TABLE
        document.getElementById('leasingTable').innerHTML = '';
        document.getElementById('ls').disabled = true;
        document.getElementById('leasingButtonText').style.color = 'Gray';
        document.getElementById('ls').checked = false;
    }
    else { // SHOW LEASING BUTTON
        if (document.getElementById('ls').disabled){
            document.getElementById('ls').disabled = false;
            document.getElementById('leasingButtonText').style.color = '#174068';
        }
    }
    if(document.getElementById('leasingTable').innerHTML!=''){ // LEASING TABLE IS VISIBLE
        document.getElementById('price_').value=(total*fixing).toFixed(2);
        //document.getElementById('price_dds').value=total_bgl.toFixed(2);                
        calc(total_bgl.toFixed(2));
    }
}

function change(id){
    active_cpu=id;
    
    document.cfgForm.cpuSelect[0].options.selectedIndex=id;
    document.cfgForm.cpuSelect[1].options.selectedIndex=id;
    
    if(id==1){
        mb_id=mb_intel_id;    
        cpu_id=cpu_intel_id;
    }
    else {
        mb_id=mb_amd_id;    
        cpu_id=cpu_amd_id;
    }
    var mb=document.cfgForm.elements['cat'+(mb_id)];
    var mb_price=document.cfgForm.elements['price'+(mb_id)];
    
    var cpu=document.cfgForm.elements['cat'+(cpu_id)];
    var cpu_price=document.cfgForm.elements['price'+(cpu_id)];
    
    
    mb_tmp=mb.parentNode.replaceChild(mb_tmp,mb);
    mb_price_tmp=mb_price.parentNode.replaceChild(mb_price_tmp,mb_price);
    cpu_tmp=cpu.parentNode.replaceChild(cpu_tmp,cpu);
    cpu_price_tmp=cpu_price.parentNode.replaceChild(cpu_price_tmp,cpu_price);
    
    calc_total();
}

function fill_price(category_name,product_id){
    //alert("cat name: "+category_name + ", product id: "+product_id);
    category_id=category_name.substr(3);
    if(product_id){
        //alert (eval(product_id));
        if (typeof document.cfgForm.elements['price'+category_id].value == 'undefined'){
            document.cfgForm.elements['price'+category_id][0].value = eval(product_id);
        }
        else{
            document.cfgForm.elements['price'+category_id].value=eval(product_id);
        }
       // if(document.cfgForm.elements['count'+category_id])
            //document.cfgForm.elements['count'+category_id].style.visibility='visible';
    }
    else{
        if (typeof document.cfgForm.elements['price'+category_id].value == 'undefined'){
            document.cfgForm.elements['price'+category_id][0].value = "";
        }
        else{
            document.cfgForm.elements['price'+category_id].value="";
        }
        //if(document.cfgForm.elements['count'+category_id])
            //document.cfgForm.elements['count'+category_id].style.visibility='hidden';
    }

    
}
function check_mb(){
    var mainboard;
    var mb_txt;
    var re_vga = /vga/i;
    var re_sb = /sb/i;
    var re_lan = /lan/i;
    if(typeof(document.cfgForm.elements['cat'+mb_intel_id])=='undefined')
        mainboard=document.cfgForm.elements['cat'+mb_amd_id];
    else
        mainboard=document.cfgForm.elements['cat'+mb_intel_id];
    
    mb_txt=mainboard.options[mainboard.options.selectedIndex].text;
    
    if(mb_txt.match(re_vga))document.getElementById('onboardVga').innerHTML='onboard';else document.getElementById('onboardVga').innerHTML='';
    if(mb_txt.match(re_sb))document.getElementById('onboardSb').innerHTML='onboard';else document.getElementById('onboardSb').innerHTML='';
    if(mb_txt.match(re_lan))document.getElementById('onboardLan').innerHTML='onboard';else document.getElementById('onboardLan').innerHTML='';
}


function check_cpu(){
    var re_box = /box/i;
    
    if(typeof(document.cfgForm.elements['cat'+cpu_intel_id])=='undefined')
        var cpu=document.cfgForm.elements['cat'+cpu_amd_id];
    else
        var cpu=document.cfgForm.elements['cat'+cpu_intel_id];
        var id_txt=cpu.options[cpu.selectedIndex].text;
    
    if(id_txt.match(re_box)){
        document.cfgForm.elements['cat'+fan_id].style.visibility='hidden';
        document.cfgForm.elements['price'+fan_id].value='';
        document.cfgForm.elements['cat'+fan_id].selectedIndex=0;
    }
    else {
        document.cfgForm.elements['cat'+fan_id].style.visibility='visible';
    }
}

function check_vga(){
	if(typeof(document.cfgForm.elements['cat'+mb_intel_id])=='undefined')
        mainboard=document.cfgForm.elements['cat'+mb_amd_id];
    else
        mainboard=document.cfgForm.elements['cat'+mb_intel_id];
        

	var mb_txt=mainboard.options[mainboard.options.selectedIndex].text;
	
	var vga_array=p[video_id];
	var vga_cat_options=document.cfgForm.elements['cat'+video_id].options;
	
	var re_vga_express = /express/i;

	if(mb_txt){
		if(mb_txt.match(re_vga_express)){
			for(i=1;i<vga_cat_options.length;i++){
				if(!vga_cat_options[i].text.match(re_vga_express))
					vga_cat_options[i].style.backgroundColor='red';
				else
					vga_cat_options[i].style.backgroundColor='white';		
			}
		}
		else{
			for(i=1;i<vga_cat_options.length;i++){
				if(vga_cat_options[i].text.match(re_vga_express))
					vga_cat_options[i].style.backgroundColor='red';	
				else
					vga_cat_options[i].style.backgroundColor='white';	
			}
		}
	}
}

function multiple_category(category_id,row_index,br){
    table=document.getElementById('cfgTable');
    
    var category_selectedIndex=document.cfgForm.elements['cat'+category_id].selectedIndex;
    var category_price=document.cfgForm.elements['price'+category_id].value;
    
    // GET DUPLICATES COUNT
    var duplicates_count = 0;
    var categories = document.cfgForm.elements['cat'];
    regex = new RegExp("^cat"+category_id+"_[0-9]+$", "");
    for (i = 0; i < categories.length; i ++){
        if (categories[i].name.match(regex)){
            duplicates_count ++;
        }
    }
    diff = br - duplicates_count - 1; 
    if (diff > 0) for(i=1;i<=diff;i++){
        
        selectHTML = table.rows[row_index].cells[1].innerHTML;
        
        selectHTML = selectHTML.replace(/name=["']?cat[0-9]+['"]?/i, "name=\"cat" + category_id + "_" + (i+duplicates_count) + "\"")
        
        new_row=table.insertRow(row_index+i+duplicates_count);
        cell0=new_row.insertCell(0);
        cell0.innerHTML=table.rows[row_index].cells[0].innerHTML;
        cell1=new_row.insertCell(1);
        cell1.innerHTML = selectHTML;
        
        cell2=new_row.insertCell(2);
        cell2.innerHTML='<input type="text" name="price'+category_id+ "_" + (i+duplicates_count) +'" id="price" value="'+category_price+'"class="inputCurrency" readonly>';
        
        cell3=new_row.insertCell(3);
        cell3.innerHTML='&nbsp;';
        
        //alert(typeof document.cfgForm.elements["cat" + category_id + "_" + (i+duplicates_count)].selectedIndex)
        if (typeof document.cfgForm.elements["cat" + category_id + "_" + (i+duplicates_count)].selectedIndex == 'undefined'){
            document.cfgForm.elements["cat" + category_id + "_" + (i+duplicates_count)][0].selectedIndex = category_selectedIndex
        }
        else{
            document.cfgForm.elements["cat" + category_id + "_" + (i+duplicates_count)].selectedIndex = category_selectedIndex
        
        }
    }
    else for (i=1;i<= Math.abs(diff);i++){
        table.deleteRow(row_index + duplicates_count - i + 1)
    }
    calc_total();
    }

function makeSelectedProductsList(){
    var selectedProducts=new Array();    
    var re_pr= /[0-9]+/g;
    catego = document.cfgForm.elements['cat'];
    if(document.getElementById('triger_a'))
       document.selectedProductsForm.innerHTML='<input type="hidden" name="'+document.getElementById('triger').name+'" value="'+document.getElementById('triger_a').value+'">';

    for(i=0;i<catego.length;i++){
        if(document.cfgForm.elements['cat'][i].options[document.cfgForm.elements['cat'][i].selectedIndex].value){
            pr=document.cfgForm.elements['cat'][i].options[document.cfgForm.elements['cat'][i].selectedIndex].value;
            selectedProducts[i]=new Array();
            category_id=pr.match(re_pr)[0];        
            product_id=pr.match(re_pr)[1];
            if(category_id>=0 && product_id>=0){    
                selectedProducts[i][0]=category_id    ;
                selectedProducts[i][1]=product_id;
                
                elem=document.createElement('INPUT');
                elem.type='hidden';
                elem.name='selectedProducts[]';
                elem.value=selectedProducts[i];
                document.selectedProductsForm.appendChild(elem);
            }
        }
    }

    if(cpu_price && cpu_tmp_){
        selectedProducts[i]=new Array();
        category_id=cpu_tmp_.match(re_pr)[0];        
        product_id=cpu_tmp_.match(re_pr)[1];
        if(category_id>=0 && product_id>=0){    
            selectedProducts[i][0]=category_id    ;
            selectedProducts[i][1]=product_id;
                
            elem=document.createElement('INPUT');
            elem.type='hidden';
            elem.name='selectedProducts[]';
            elem.value=selectedProducts[i];
            document.selectedProductsForm.appendChild(elem);
        }    
        
    }
    
    elem=document.createElement('INPUT');
    elem.type='hidden';
    elem.name='selectedLeasing';
    if(document.getElementById('leasingTable').innerHTML!=''){
        for(a=0;a<document.leasingForm.leasing_limit.length;a++){
            if(document.leasingForm.leasing_limit[a].checked)
                elem.value=document.leasingForm.leasing_limit[a].value;
        }    
    }    
    else 
        elem.value=false;

    document.selectedProductsForm.appendChild(elem);
    
    if(selectedProducts.length==0)alert('Please, chose a product!');
    else {

        document.selectedProductsForm.submit();
        
        }
}

function checkLs(){
    if(document.getElementById('ls').checked)showLeasing();
    else hideLeasing();    
}

function showLeasing(){
    total=Math.abs(document.cfgForm.total.value);
    //fixing=Math.abs(document.getElementById('course').innerHTML);
    total_bgl=total*fixing;
    document.getElementById('leasingTable').innerHTML=leasing;
    //document.getElementById('price_dds').value=total_bgl.toFixed(2);
    document.getElementById('price_').value=(total*fixing).toFixed(2);
    calc(total_bgl.toFixed(2));
}

function hideLeasing(){
    document.getElementById('leasingTable').innerHTML='';
}

var oldRowId=false;
function colorRow(row){
    if(oldRowId){
        document.getElementById('calcTable').rows[oldRowId].style.backgroundColor="#FFFFFF";
        document.getElementById('calcTable').rows[oldRowId].style.color="#174068";}
    
    id=row.rowIndex-2;
    if (typeof(document.leasingForm.leasing_limit) != 'undefined'){    
        document.leasingForm.leasing_limit[id].checked=true;
    }
    row.style.backgroundColor="#F1656E";    
    row.style.color="#FFFFFF";
    oldRowId=row.rowIndex;
}    
    
function calc(price){//price is free of DDS
    var calcTable=document.getElementById('calcTable');
    var months=new Array(3,6,9,12,15,18,24);    
    var increase=new Array(5,8,10.8,13.8,16.5,19.4,24.8);
    var monthly_serve_tax=1.80;
    var monthly_credit_control_tax=0.3;
    var monthly_payment=Array();    

    if(isNaN(price))price=0;

    var len=months.length;
    var price_dds=price*1.2;

    //document.getElementById('price_').value=price;
    document.getElementById('price_dds').innerHTML=price_dds.toFixed(2);
    
    if (typeof(calcTable.rows[2].cells[4]) != 'undefined'){
        index_of_monthy_payment = 3;
        index_of_total_cost = 4;
    }
    else{
        index_of_monthy_payment = 2;
        index_of_total_cost = 3;
    }
    
    if(!isNaN(price) && price>0){
        for(i=0;i<len;i++){
            monthly_payment[i]=(price_dds*(1+0.01*increase[i])+months[i]*(monthly_serve_tax+(monthly_credit_control_tax*price_dds*0.01)))/months[i];
            calcTable.rows[i+2].cells[index_of_monthy_payment].innerHTML=monthly_payment[i].toFixed(2) + ' ' + currency + '.';
            calcTable.rows[i+2].cells[index_of_total_cost].innerHTML=(monthly_payment[i]*months[i]).toFixed(2) + ' ' + currency + '.';
        }
    }
    else{
        for(i=0;i<len;i++){
            calcTable.rows[i+2].cells[index_of_monthy_payment].innerHTML='&nbsp;';
            calcTable.rows[i+2].cells[index_of_total_cost].innerHTML='&nbsp;';
        }
    }
}

function validateClientInfo(){
    clientName=document.getElementById('clientName').value;
    clientPhone=document.getElementById('clientPhone').value;
    clientMail=document.getElementById('clientMail').value;
    
    
    var re_name = /^[A-Za-zА-Яа-я]+( [A-Za-zА-Яа-я]+)+$/i;
    var re_phone = /^[^A-Za-zА-Яа-я]{5,20}$/i;
    var re_mail = /^[a-z0-9_\.-]+@([a-z0-9]+([\-\_]+[a-z0-9]+)*\.)+[a-z]{2,7}$/i;
    var re_en =/(first)/i;
    if(document.getElementById('contactTable').rows[0].cells[0].innerHTML.match(re_en))lang='en';
    else lang='bg';
    var msg=new Array();
    if(!clientName.match(re_name)){
        msg['en']='Please, enter correct "First And Last Name"!';
        msg['bg']='Моля, въведете коректни "Име и Фамилия"!';
        alert(msg[lang]);
        document.getElementById('clientName').focus();
        return false;    
    }
    if(!clientPhone.match(re_phone) && !clientMail.match(re_mail)){
        msg['en']='Please, enter correct phone number or e-mail!';
        msg['bg']='Моля, въведете коректни "Телефон или E-mail"!';
        alert(msg[lang]);
        document.getElementById('clientPhone').focus();
        return false;    
    }
    return true;
    //document.contactForm.submit();
}



