function Code(code, firstText, lastText) {
    if (code != "")
	{
        insertCode = prompt(firstText, lastText);
        if ((insertCode != null) && (insertCode != ""))
        {
            document.getElementById("coded").value += "[" + code + "]" + insertCode + "[/" + code + "]";
        }
	}
	
	document.getElementById("coded").focus();
}

function toggle(id)
{
    el = document.getElementById(id);
    var display = el.style.display ? '' : 'none';
    el.style.display = display;
}

function roc_replacepic(source)
{
        document.getElementById('roc-color').src = source
}

function replace_cat_series(np)
{
    var stop = 0
    var prev_currseries = currseries
    if(np == 'n' && (currseries+1) <= total_series) currseries = currseries + 1; else if(np == 'p' && (currseries-1) > 0) currseries = currseries - 1; else stop = 1;
    if(stop == 0)
    {
        document.getElementById('cats_series['+prev_currseries+']').style.display = 'none'
        document.getElementById('cats_series['+currseries+']').style.display = ''
        prev_currseries = 0
    }
}

function addons_mouse_events(obj, event, acat, addon)
{
    if(event == 'mouseover')
    {
        if(obj.className == 'radiorow')
        {
            obj.className = 'radiorow_over';
        }
        else if(obj.className == 'radiorow_selected')
        {
            obj.className = 'radiorow_selected';
        }
    }
    else if(event == 'mouseout')
    {
        if(obj.className == 'radiorow_over')
        {
            obj.className = 'radiorow';
        }
        else if(obj.className == 'radiorow_selected')
        {
            obj.className = 'radiorow_selected';
        }
    }
    else if(event == 'mouseclick')
    {
        document.getElementById(addon_last_eid_selected[acat]).className = 'radiorow';
        addon_last_eid_selected[acat] = 'rrow_'+acat+'_'+addon;
        addon_last_id_selected[acat] = addon;
        
        if(document.getElementById('roption_'+acat+'_'+addon).checked == false)
        {
            document.getElementById('roption_'+acat+'_'+addon).checked = true;
        }
        
        if(obj.className == 'radiorow_over')
        {
            obj.className = 'radiorow_selected';
        }
        else if(obj.className == 'radiorow')
        {
            obj.className = 'radiorow_selected';
        }
        else if(obj.className == 'radiorow_selected')
        {
            obj.className = 'radiorow_selected';
        }
        
        update_name_price();
        
    }
}

function addCommas(nStr)
{
    /*
    ****
    * From http://www.mredkj.com/javascript/nfbasic.html
    ****
    */
    
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

function update_name_price() {
var x;
    product_new_price = product_original_price;
    document.getElementById("chosen_addons_title").style.display = '';
    document.getElementById("chosen_addons").innerHTML = '';
    for (x in acat_list)
    {
        if(addons_prices[addon_last_id_selected[acat_list[x]]] > 0)
        {
            product_new_price = product_new_price + addons_prices[addon_last_id_selected[acat_list[x]]];
            document.getElementById("chosen_addons").innerHTML += "<LI>" + addons_names[addon_last_id_selected[acat_list[x]]] + "</LI>\n";
        }
    }

    if(document.getElementById("chosen_addons").innerHTML == '')
    {
        document.getElementById("chosen_addons_title").style.display = 'none';
    }
    else
    {
        document.getElementById("chosen_addons").innerHTML = '<UL id="chosen_addons" class="chosen_addons">'+document.getElementById("chosen_addons").innerHTML+'</UL>';
    }

    document.getElementById("finalprice").innerHTML = addCommas(product_new_price);
    document.addons.pfinalprice.value = product_new_price;
}

function agreement_popup()
{
    agreement=window.open("", "agreementwin", "width=640,height=480,toolbar=0");
    agreement.document.open();
    agreement.document.write('<html dir="rtl"><head><title>הסכם שימוש</title></head><body>הסכם השימוש... בלה בלה בלה<br /><a href="javascript:window.close()">סגור חלון</a></body></html>');
    agreement.document.close();
    agreement.focus();
}

function searchinput(element, name, placeholder) {
	var SearchElem = document.getElementById(element);
	if (navigator.userAgent.indexOf("WebKit") != -1) {
	   
		SearchElem.setAttribute("type", "search");
		SearchElem.setAttribute("autosave", name);
		SearchElem.setAttribute("results", "10");
		SearchElem.setAttribute("placeholder", placeholder);
	}
}

function validatefield(element, type)
{
    if(type == 'password')
    {
        if(element.value.length < 6 && element.value.length > 0)
        {
            document.getElementById(type + '_validate').innerHTML = 'הסיסמא צריכה להיות 6 תווים או יותר';
        }
        else
        {
            document.getElementById(type + '_validate').innerHTML = '';
        }
    }
    else if(type == 'username')
    {
        if(element.value.length < 3 && element.value.length > 0)
        {
            document.getElementById(type + '_validate').innerHTML = 'הכינוי צריך להיות 3 תווים או יותר';
        }
        else
        {
            document.getElementById(type + '_validate').innerHTML = '&nbsp;';
        }
    }
    else if(type == 'email')
    {
        if((element.value.indexOf('@') == -1 || element.value.length < 6) && element.value.length > 0)
        {
            document.getElementById(type + '_validate').innerHTML = 'האי-מייל שהזנת אינו תקין';
        }
        else
        {
            document.getElementById(type + '_validate').innerHTML = '&nbsp;';
        }
    }
}

function prods_mainpage_replace(page) {
    var imgpath = 'images/products/index3/mptb/';
    if(page == 'selected_prods')
    {
        document.getElementById('selected_prods').style.display='';
        document.getElementById('categories').style.display='none';
        document.getElementById('manfs').style.display='none';
        document.getElementById('selected_prods_img').src=imgpath+"selected_prods_on.gif";
        document.getElementById('categories_img').src=imgpath+"categories.gif";
        document.getElementById('manfs_img').src=imgpath+"manfs.gif";
    }
    else if(page == 'categories')
    {
        document.getElementById('selected_prods').style.display='none';
        document.getElementById('categories').style.display='';
        document.getElementById('manfs').style.display='none';
        document.getElementById('selected_prods_img').src=imgpath+"selected_prods.gif";
        document.getElementById('categories_img').src=imgpath+"categories_on.gif";
        document.getElementById('manfs_img').src=imgpath+"manfs.gif";
    }
    else if(page == 'manfs')
    {
        document.getElementById('selected_prods').style.display='none';
        document.getElementById('categories').style.display='none';
        document.getElementById('manfs').style.display='';
        document.getElementById('selected_prods_img').src=imgpath+"selected_prods.gif";
        document.getElementById('categories_img').src=imgpath+"categories.gif";
        document.getElementById('manfs_img').src=imgpath+"manfs_on.gif";
    }
}

function uncheck_mcs(type, num) {
    for(var i=1; i<=num; i++)
    {
        if(document.getElementById(type+"_"+i))
            document.getElementById(type+"_"+i).checked=false;
    }
}

function addcountlink(id) {
    count = parseInt(document.getElementById('count_id_'+id).value);
    if(count == 0)
    {
        if(!(confirm('האם אתה בטוח שברצונך למחוק מוצר זה מהרשימה?')))
        {
            return false;
        }
    }
    
    window.location = 'index.php?page=products&sub=updatecount&id='+id+'&count='+count+'';
}

function change_serials(sele) {
    if(sele.value == "6")
    {
        document.getElementById('5serials').style.display = '';
        document.getElementById('oneserial').style.display = 'none';
    }
    else
    {
        document.getElementById('oneserial').style.display = '';
        document.getElementById('5serials').style.display = 'none';
    }
}
