/**
 * Unblock all form inputs
 */
function unblockMainPage(){
	var arrElements = new Array('input','select');
	for(id in arrElements){
		emType = arrElements[id];
        var inputs = document.getElementsByTagName(emType);
        for(inp in inputs){
            var elem = document.getElementsByTagName(emType)[inp];
            if (elem.value != 'disabled') {
	            if (elem.style) elem.style.visibility = "visible";
	            elem.readonly == false;
	            elem.disabled = false;
            } else elem.disabled = true;
		}
	}
}

/**
 * Hide bob message
 */
function hideBobTimeout(){
	setTimeout("document.getElementById('bob').style.display = 'none'", 6000);
}

/**
 * Check inputs values
 */
function checkInputs(){
	var submitObj = document.getElementById('pickup_string');
	if (submitObj.disabled == true){
		unForm();
	}
}

function clearInput(inputName, pagePartName){
    document.getElementById(inputName).value='';
    hideForm(pagePartName);
}

function hideForm(pagePartName){
    vista = (document.getElementById(pagePartName).style.visibility == 'hidden') ? 'visible' : 'hidden';
    document.getElementById(pagePartName).style.visibility = vista;
}

function setReturn(){
    document.getElementById('retDepCity').value=document.getElementById('pckDepCity').value;
}
    
function submitter(){
	search_form.submit();
    disableForm();
    document.getElementById('cal_button1').src = 'templates/default/files/spacer.gif';
    document.getElementById('cal_button2').src = 'templates/default/files/spacer.gif';
}


function getChildImage( contextElm ) {
    contextElm = contextElm.firstChild;
    while(contextElm.nodeName.toLowerCase() != 'img' && contextElm.nextSibling)
        contextElm = contextElm.nextSibling;
    return contextElm;
}


function RegenerateDate(select_box_name, from_month){
	var destList = window.document.forms[0][select_box_name];
	/**
     * add remove days in month
     */
    var NS4 = (navigator.appName == "Netscape" && parseInt(navigator.appVersion) < 5);
    var limit_ = 31;
	// days limit
	if (from_month==1) limit_ = 28;
	if (from_month==2) limit_ = 31;
	if (from_month==3) limit_ = 30;
	if (from_month==4) limit_ = 31;
	if (from_month==5) limit_ = 30;
	if (from_month==6) limit_ = 31;
	if (from_month==7) limit_ = 31;
	if (from_month==8) limit_ = 30;
	if (from_month==9) limit_ = 31;
	if (from_month==10) limit_ = 30;
	if (from_month==11) limit_ = 31;

	destList.options.length = 0;
	destList.options.length = (limit_-1);

	for (var k=0; k < limit_; k++){
		if (k < 9) {
			var newOpt = new Option('0'+(k+1), '0'+(k+1));
			destList.options[k] = newOpt;
		}
		else {
			var newOpt = new Option(k+1, k+1);
			destList.options[k] = newOpt;
		}
	}
	if(NS4) history.go(0);
}

function is_correct_time(search_form)
{
    var from_day = document.search_form.pickup_Day.options.selectedIndex;
    var from_month = document.search_form.pickup_Month.options.selectedIndex;
    var from_year = document.search_form.pickup_Year.options.selectedIndex;
    var to_day = document.search_form.return_Day.options.selectedIndex;
    var to_month = document.search_form.return_Month.options.selectedIndex;
    var to_year = document.search_form.return_Year.options.selectedIndex;

    var from_hour = document.getElementsByName('startTimeHour')[0].options.selectedIndex;
    var from_minute = document.getElementsByName('startTimeMinute')[0].options.selectedIndex;
    var to_hour = document.getElementsByName('endTimeHour')[0].options.selectedIndex;
    var to_minute = document.getElementsByName('endTimeMinute')[0].options.selectedIndex;

    if ((to_year == from_year) && (to_month == from_month) && (to_day == from_day))
    {
        if(from_hour == to_hour)
        {
            if(to_minute <= from_minute) return false;
        } else if (from_hour > to_hour) return false;
    }
    return true;
}

function set_correct_to_date(search_form) {
	var from_day = document.search_form.pickup_Day.options.selectedIndex;
	var from_month = document.search_form.pickup_Month.options.selectedIndex;
	var from_year = document.search_form.pickup_Year.options.selectedIndex;
	var to_day = document.search_form.return_Day.options.selectedIndex;
	var to_month = document.search_form.return_Month.options.selectedIndex;
	var to_year = document.search_form.return_Year.options.selectedIndex;

	RegenerateDate('return_Day', to_month);

    if ((to_year == from_year) && (to_month == from_month) && (to_day < from_day)) {
        to_month = from_month + 1;
    }
    if (((to_year == from_year) && (to_month < from_month)) || (to_year < from_year)) {
        to_year++;
    }
    // avoiding problem with diffrent number of days in every month - setting 1 day of next month
    if (to_day >= getDaysNum(to_month + 1, actYear + to_year)) {
        to_day = 0;
        to_month++;
    }
    if (to_month > 11) {
        to_month = 0;
        to_year++;
    }
    if (to_year > 2) {
        to_day = 30;
        to_month = 11;
        to_year = 2;
    }
    if (document.search_form.return_Day[to_day]) document.search_form.return_Day[to_day].selected = true;
    if (document.search_form.return_Month[to_month]) document.search_form.return_Month[to_month].selected = true;    
    if (document.search_form.return_Year[to_year]) document.search_form.return_Year[to_year].selected = true;
    getDayName();
}

function getDayName(){
	/*
	xajax_getDayName(xajax.getFormValues('search_form'));
	*/
}



/**
 * Getting element dimensions
 */
function getDimensions( elm ) {
    var box = { x:0, y:0, w:0, h:0 };
    if(document.getBoxObjectFor) {
        var boxRef = document.getBoxObjectFor(elm);
        box.x = boxRef.x;
        box.y = boxRef.y;
        box.w = boxRef.width;
        box.h = boxRef.height;
    }
    else if(elm.getBoundingClientRect) {
        var rxIE50 = /MSIE 5\.0/g;
        //alert(rxIE50 + '.test("' + navigator.appVersion + '" = ' + rxIE50.test(navigator.appVersion));
        var boxRef = elm.getBoundingClientRect();
        box.x = boxRef.left;
        box.y = boxRef.top;
        box.w = (boxRef.right - boxRef.left);
        box.h = (boxRef.bottom - boxRef.top);
        /* Damn IE... */
        if(document.compatMode && document.compatMode != 'BackCompat') {
            // IE6/compliance mode
            box.x += document.documentElement.scrollLeft - 2;
            box.y += document.documentElement.scrollTop - 2;
        }
        else {
            // IE5.5
            box.x += document.body.scrollLeft - 2;
            box.y += document.body.scrollTop - 2;
        }
    }
    else {
    	/*
         * No known box information available, walking
         * manually through offsetParents to calculate x/y coordinates
         */
        box.w = elm.offsetWidth;
        box.h = elm.offsetHeight;
        while(elm) {
            box.x += elm.offsetLeft;
            box.y += elm.offsetTop;
            if(elm.offsetParent) // Required for Safari 1.3 :(
                elm = elm.offsetParent;
            else
                break;
        }
    }
    var cc;
    if(cc = document.getElementById('bodyconstraint'))
        box.x -= cc.offsetLeft;
    return box;
}

function setHidStrings(){
    addPlace('dep_string','pckDepPlace', 'pckDep', 'pckDepCity');
    addPlace('ret_string','retDepPlace', 'pckDep', 'retDepCity');
}

function expireMessage(){
	/*
	var dataString = '<a href="index.php?p=cls&aid={/literal}{$aid}" />{$langTab.session_expired_msg.phrase}{literal}</a>';
	*/
    document.getElementById("expireCounterTable").style.background = "#ff3939";
    document.getElementById("expireCounterTable").style.color = "#eeffee";
    document.getElementById("expTime").innerHTML = "";
    document.getElementById('counterPic').style.width = 0;
}

/**
 * Check does browser supports ajax
 */
function checkForTheBrowserAbility(){
	var redirection = false;
	if (window.ActiveXObject) { // IE
	    try {
	        http_request = new ActiveXObject("Msxml2.XMLHTTP");
	    } catch (e) {
	        try {
	            http_request = new ActiveXObject("Microsoft.XMLHTTP");
	        } catch (e) { redirection = true; }
	    }
    }
    if (redirection == true) {
    	var agent_id = document.getElementById('profile_agent');
		parent.location.href = '../car_frontend_simple/?aid='+agent_id.value;
	}
	else {
		correctDate();
		timeCounter();
		determineBrowser();
		getDayName();
		unblockMainPage();
	}
}