function ref(object)
{
    if (document.getElementById)
    {
        return document.getElementById(object);
    }
    else if (document.all)
    {
        return eval('document.all.' + object);
    }
    else
    {
        return false;
    }
}

function select_all (obj) {
    var ids = document.getElementsByName(obj);
    for (i=0;i<ids.length ;i++ ) {
        ids[i].checked = 'checked';
    }
}
function select_none (obj) {
    var ids = document.getElementsByName(obj);
    for (i=0;i<ids.length ;i++ ) {
        ids[i].checked = '';
    }
}

function removeAllOptions(selectbox)
{
	var i;
	for(i=selectbox.options.length-1;i>=0;i--)
	{
		alert(i);
		//selectbox.options.remove(i);
		selectbox.remove(i);
	}
}


function addOption(selectbox, value, text )
{
	var optn = document.createElement("OPTION");
	optn.text = text;
	optn.value = value;
	selectbox.options.add(optn);
}

function setms(fmarka,fmodel) {

 if ( (fmarka)&&(fmodel)) {
	marka='';
	 if ( fmarka.selectedIndex>=0 )
		marka=fmarka.options[fmarka.selectedIndex].value;
	 fmodel.length=0;

//	 marki=ref('marki').innerHTML.split('**');
	 var marki = new Object();
	 marki=document.getElementById('marki').innerHTML.split('**');
	 i=0;

     removeAllOptions(fmodel);
	 while ( (i<marki.length)&&(fmodel.length==0) ) {

         marki[i]=marki[i].split('~');
		 if ( marki[i][0]==marka ) {
			 k=1;
			 while ( k<marki[i].length ) {
                 optval=marki[i][k];

                 if ( (marka=='Mercedes')&&(optval.length==1) )
                     optval=optval+'-класа'
                 if ( (marka=='Bmw')&&(optval.length==1) )
                     optval=optval+'-серия'
                 optname = optval;
                 if ( optval=='' )
                     optname='[моля изберете]';
                 addOption(fmodel, optval, optname);
//				 fmodel.options[k-1]=new Option(optval,marki[i][k]);
				 k++;
			 }
		 }
		 i++;
	 }
	 if ( fmodel.length==0 )
		 addOption(fmodel, "", '', 'няма модели');
//		 fmodel.options[0]=new Option('','');
 }
}



function jumpMenu(url){
        window.location.href = url;
}

// Select from dropDown be given value
function select_dropdown_value(dropdown, value) {
    if (!dropdown) return false;
    lgth = dropdown.options.length;
    if (dropdown.id == 'model') {
        setms($('marka'),$('model'), value);
    }

    for (var i=0;i<lgth;i++) {
//        if (dropdown.id == 'model') alert("NAME: " + dropdown.name + "\n Dropdown length: " + lgth + "\n CONDITION: " + dropdown.options[i].value + " == " + value);
        if (dropdown.options[i].value == value) {
            dropdown.selectedIndex = i;
        }
    }
    return true;
} 

//Check the proper features on a given $string of features
function select_features_value(sel_features) {

    var features = document.getElementsByName('features[]');
    for (var i=0;i< features.length;i++ ) {
        var re = new RegExp(features[i].value);
        if (sel_features.match(re)) {
            features[i].checked= "checked";
        }
    }
}
//ORDER AJAX
function order_set(url) {

var opt = {
    // Use POST
    // Handle successful response
     method:'post' ,
    onSuccess: function(t) {

        eval(t.responseText);
        if (jsarray['error'] != '') {
            alert(jsarray['error']);
        } else {
            $('order_items').value = jsarray['order_items'];
            if (confirm(jsarray['success_msg'])) {
                window.location.href = jsarray['success_url'];
            }
        }
/*
        if (t.responseText.indexOf('error') != -1) {
            alert  ("Контактът не може да бъде изтрит!");
        }
*/
    },
    // Handle 404
    on404: function(t) {
        alert('Error 404: location "' + t.statusText + '" was not found.');
    },
    // Handle other errors
    onFailure: function(t) {
        alert('Error ' + t.status + ' -- ' + t.statusText);
    }
}

    new Ajax.Request(url, opt);
}
function AjaxRequest(url) {
    if (url == ''){
        alert("NO URL specified in AjaxRequest");
        return false;
    }
var opt = {
    // Use POST
    // Handle successful response
     method:'post' ,
    onSuccess: function(t) {
//        eval(t.responseText);
//        if (jsarray['error'] != '') {
//            alert(jsarray['error']);
//        } else {
//            $('order_items').value = jsarray['order_items'];
//            if (confirm(jsarray['success_msg'])) {
//                window.location.href = jsarray['success_url'];
//            }
//        }
    },
    // Handle 404
    on404: function(t) {
        alert('Error 404: location "' + t.statusText + '" was not found.');
    },
    // Handle other errors
    onFailure: function(t) {
        alert('Error ' + t.status + ' -- ' + t.statusText);
    }
}

    new Ajax.Request(url, opt);
}

function setCookie(c_name,value,expiredays)
{
    if (value == 'none'){
        value = '';
    } else {
        value = 'none';
    }
var exdate=new Date()
exdate.setDate(exdate.getDate()+expiredays)
document.cookie=c_name+ "=" +value+
((expiredays==null) ? "" : ";expires="+exdate.toGMTString())
}

// Delete image from gallery
function delete_image(url, obj) {
var opt = {
    // Use POST
    // Handle successful response
    onSuccess: function(t) {
    },
    // Handle 404
    on404: function(t) {
        alert('Error 404: location "' + t.statusText + '" was not found.');
    },
    // Handle other errors
    onFailure: function(t) {
        alert('Error ' + t.status + ' -- ' + t.statusText);
    }
}

    new Ajax.Updater(obj, url, opt);
}


// RATING functions
function setStars(starNum, itemId, voted) {
    url = env.INDEX_URL + "?" + env.mainparam + "=items&items=ranking&item=" + itemId + "&rank=" + starNum + "&voted=" + voted;
    if ($('item_rating')){
        new Ajax.Updater('item_rating', url, {onComplete:function(){ new Effect.Shake('item_rating');},asynchronous:true});
    } else {
        new Ajax.Request(url, {asynchronous:true});
    }
//    AjaxRequest(url);
}
function showStars(starNum, itemId) {
    if (!itemId){
        itemId = '';
    }
    for(var i=1;i<=env.PH_MAX_RATING_NUM;i++){
        if (i <= starNum) {
            $("star_" + itemId + "_"+(i)).src = "images/icons/small/ranking_star_yellow.png";
        } else {
            $("star_" + itemId + "_"+(i)).src = "images/icons/small/ranking_star_grey.png";
        }
    }
}
function resetStars(rank,itemId) {
    if (!itemId){
        itemId = '';
    }
    for(var i=1;i<=env.PH_MAX_RATING_NUM;i++){
        if (i <= rank) {
            $("star_" + itemId + "_"+(i)).src = "images/icons/small/ranking_star_yellow.png";
        } else if (i == Math.round(rank)){
            $("star_" + itemId + "_"+(i)).src = "images/icons/small/ranking_star_middle.png";
        } else {
            $("star_" + itemId + "_"+(i)).src = "images/icons/small/ranking_star_grey.png";
        }
    }
}

/*
//Disable right mouse click Script
//By Maximus (maximus@nsimail.com) w/ mods by DynamicDrive
//For full source code, visit http://www.dynamicdrive.com

var message="";
///////////////////////////////////
function clickIE() {if (document.all) {(message);return false;}}
function clickNS(e) {if 
(document.layers||(document.getElementById&&!document.all)) {
if (e.which==2||e.which==3) {(message);return false;}}}
if (document.layers) 
{document.captureEvents(Event.MOUSEDOWN);document.onmousedown=clickNS;}
else{document.onmouseup=clickNS;document.oncontextmenu=clickIE;}

document.oncontextmenu=new Function("return false")


//form tags to omit in NS6+:
var omitformtags=["input", "textarea", "select"]

omitformtags=omitformtags.join("|")

function disableselect(e){
if (omitformtags.indexOf(e.target.tagName.toLowerCase())==-1)
return false
}

function reEnable(){
return true
}

if (typeof document.onselectstart!="undefined")
document.onselectstart=new Function ("return false")
else{
document.onmousedown=disableselect
document.onmouseup=reEnable
}
*/


//  End -->

