﻿$(document).ready (init);
function init()
{
    $("div#menu ul li").not(".selected")
	.css({ backgroundPosition: "0 0" })
	    .mouseover(function() {
	        $(this).stop().animate(
			    { backgroundPosition: "(0 -2934px)" },
			    { duration: 1000 })
	    })
	    .mouseout(function() {
	        $(this).stop().animate(
			    { backgroundPosition: "(0 0)" },
			    { duration: 600 })
	    });

    $("div#content-right ul li").not(".selected")
    .mouseover(function() {
        $(this).addClass("hover");
        $(this).children().addClass("hover");
    })
    .mouseout(function() {
        $(this).removeClass("hover");
        $(this).children().removeClass("hover");
    });
    if ($("#subTestDrive, #subContact, #leftFormSubmit").length > 0) {
        $("input#subTestDrive, input#subContact, input#leftFormSubmit").bind("mouseover", function() {
            $(this).addClass("hover");
        });
        $("input#subTestDrive, input#subContact, input#leftFormSubmit").bind("mouseout", function() {
            $(this).removeClass("hover");
        });
        $("input#subTestDrive, input#subContact, input#leftFormSubmit").bind("mousedown", function() {
            $(this).addClass("down");
        });
        $("input#subTestDrive, input#subContact, input#leftFormSubmit").bind("mouseup", function() {
            $(this).removeClass("down");
        });
    }
    if ($("#contactFrm").length > 0) {
        $(this).bind("submit", function(e) { checkFrm(this.id); return false; });
    }
    if ($("#leftForm").length > 0) {
        $(this).bind("submit", function(e) { checkFrm(this.id); return false; });
    }
    if ($("#testDriveFrm").length > 0) {
        $(this).bind("submit", function(e) { checkFrm(this.id); return false; });
    }

    if ($("body#homepageDoc").length > 0) {
        checkURI()
    }
    
    if ($("#showGallery").length > 0) {
        $("a[rel^='prettyPhoto']").prettyPhoto();
    }
    if ($("#color-thumbs").length > 0) {
        $("ul#color-thumbs li a").bind("click", function() {
            var tmp = this.href;
            $("img#carColor").attr("src", tmp);
            return false;
        });
    }
    if ($("#acr-wraper").length > 0) {
        $("div.acr-category h4").not("h4.first").addClass("off");
        $("div.acr-category div.acr-collapsed").hide();
        $("div.acr-category h4").bind("click", function() {
            $("div.acr-category h4").addClass("off");
            tmp = this.id;
            $(this).removeClass("off");
            tmp = tmp.split("-");
            tmpStr = "div#div-" + tmp[1].toString();
            $("div.acr-category div").not(tmpStr).hide();
            $("html,body").animate({ scrollTop: 360 }, 1000);
            $(tmpStr).slideDown(1000);
        });
    }

	$(document).pngFix(); 
}


function checkFrm(frm) {
    var v = ($("input:hidden")[0].value);
    switch (v) {
        case "3":
            subFrmTestDrive(frm);
            break;
        case "1":
            subFrmCntact(frm);
            break;
        case "5":
            subFrmTestDrive(frm);
            break;
        default:
            alert("nothing");
            break;
    }
}

function subFrmTestDrive(frm) {
    el = $("#brandId")[0];
    if (isBlank(el.value)) {
        setError(el);
        return;
    }
    else {
        clearError(el);
    }
    el = $("#nameStr")[0];
    if (isBlank(el.value)) {
        setError(el);
        return;
    }
    else {
        clearError(el);
    }
    el = $("#telStr")[0];
    if (isBlank(el.value)) {
        setError(el);
        return;
    }
    else {
        clearError(el);
    }
    document.getElementById(frm).submit()
}

function subFrmCntact(frm) {
    el = $("#sbjStr")[0];
    if (isBlank(el.value)) {
        setError(el);
        return;
    }
    else {
        clearError(el);
    }
    el = $("#nameStr")[0];
    if (isBlank(el.value)) {
        setError(el);
        return;
    }
    else {
        clearError(el);
    }
    el = $("#telStr")[0];
    if (isBlank(el.value)) {
        setError(el);
        return;
    }
    else {
        clearError(el);
    }
    document.getElementById(frm).submit()
}

function getLC(caseStr) {
    var res, enrgy, energyArr = document.getElementsByName("energy");
    showLoad(1)
    /*
    for(i=0;i<energyArr.length;i++)
    {
    if(energyArr[i].checked) {
    energy = energyArr[i].value;
    }
    }
    */
    energy = "dizel";
    var lng, lngArr = document.getElementsByName("lng");
    for (i = 0; i < lngArr.length; i++) {
        if (lngArr[i].checked) {
            lng = lngArr[i].value;
        }
    }
    populateLC(caseStr, lng, energy, resLC)
}
function populateLC(caseStr, lng, energy, respFunction) {
    if (caseStr != "" && respFunction != "") {
        var w3Ajax = new tempAjax();
        var strUrl = "http://"+location.host+"/lc/?caseStr="+caseStr+"&lng="+lng+"&energy="+energy;
        w3Ajax.doGet(strUrl, respFunction, "text");
        if (!document.all && w3Ajax.readyState < 4) {
            //alert("FireFox has a strange bug")
        }
    }
}
function resLC(res) {
    var dvTarget = document.getElementById("LCDATA");
    dvTarget.innerHTML = "";
    dvTarget.innerHTML = res;
    $("div.acr-category h4").not("h4.first").addClass("off");
    $("div.acr-category div.acr-collapsed").hide();
    $("div.acr-category h4").bind("click", function() {
	    $("div.acr-category h4").addClass("off");
        tmp = this.id;
        $(this).removeClass("off");
        tmp = tmp.split("-");
        tmpStr = "div#div-" + tmp[1].toString();
        $("div.acr-category div").not(tmpStr).hide();
        $("html,body").animate({ scrollTop: 360 }, 1000);
        $(tmpStr).slideDown(1000);
    });
    showLoad(0)
}
function showLoad(a) {
    if (a == 1) {
        document.getElementById('loading-indicator').style.display = 'block';
    }
    else {
        document.getElementById('loading-indicator').style.display = 'none';
    }
}

//forms
String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/gm, '');
}

function setErrorNoFocus(el) {
    cssjs("add", el, "validateError", "")
}
function setError(el) {
    cssjs("add", el, "validateError", "")
    el.focus();
}
function clearError(el) {
    cssjs("remove", el, "validateError", "")
}


function cssjs(a, o, c1, c2) {
    switch (a) {
        case 'swap':
            o.className = !cssjs('check', o, c1) ? o.className.replace(c2, c1) : o.className.replace(c1, c2);
            break;
        case 'add':
            if (!cssjs('check', o, c1)) { o.className += o.className ? ' ' + c1 : c1; }
            break;
        case 'remove':
            var rep = o.className.match(' ' + c1) ? ' ' + c1 : c1;
            o.className = o.className.replace(rep, '');
            break;
        case 'check':
            return new RegExp('\\b' + c1 + '\\b').test(o.className)
            break;
    }
}

function checkEmail(val) {
    if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(val)) {
        return true;
    }
    else {
        return false;
    }
}

function isValidEmail(el) {
    var reEmail = /^([\w]+)(.[\w]+)*@([\w]+)(.[\w]{2,3}){1,2}$/;
    if (el) {
        var re = new RegExp(reEmail);
        el.value = el.value.trim();
        if (el.value == "" || !el.value.match(re)) {
            return false
        }
        else {
            return true
        }
    }
}

function LTrim(str) {
    if (str == null) { return null; }
    for (var i = 0; str.charAt(i) == " "; i++);
    return str.substring(i, str.length);
}
function RTrim(str) {
    if (str == null) { return null; }
    for (var i = str.length - 1; str.charAt(i) == " "; i--);
    return str.substring(0, i + 1);
}
function Trim(str) { return LTrim(RTrim(str)); }
function LTrimAll(str) {
    if (str == null) { return str; }
    for (var i = 0; str.charAt(i) == " " || str.charAt(i) == "\n" || str.charAt(i) == "\t"; i++);
    return str.substring(i, str.length);
}
function RTrimAll(str) {
    if (str == null) { return str; }
    for (var i = str.length - 1; str.charAt(i) == " " || str.charAt(i) == "\n" || str.charAt(i) == "\t"; i--);
    return str.substring(0, i + 1);
}
function TrimAll(str) {
    return LTrimAll(RTrimAll(str));
}
function isNull(val) { return (val == null); }
function isBlank(val) {
    if (val == null) { return true; }
    for (var i = 0; i < val.length; i++) {
        if ((val.charAt(i) != ' ') && (val.charAt(i) != "\t") && (val.charAt(i) != "\n") && (val.charAt(i) != "\r")) { return false; }
    }
    return true;
}
function isInteger(val) {
    if (isBlank(val)) { return false; }
    for (var i = 0; i < val.length; i++) {
        if (!isDigit(val.charAt(i))) { return false; }
    }
    return true;
}
function isNumeric(val) { return (parseFloat(val, 10) == (val * 1)); }
function isArray(obj) { return (typeof (obj.length) == "undefined") ? false : true; }
function isDigit(num) {
    if (num.length > 1) { return false; }
    var string = "1234567890";
    if (string.indexOf(num) != -1) { return true; }
    return false;
}
function setNullIfBlank(obj) { if (isBlank(obj.value)) { obj.value = ""; } }
function setFieldsToUpperCase() {
    for (var i = 0; i < arguments.length; i++) {
        arguments[i].value = arguments[i].value.toUpperCase();
    }
}

function checkURI() {
    if (document.location.search != "") {
        var tmp = document.location.search.toString();
        if (tmp.indexOf("suc=1") > 0) {
            $("#headerLogos").append("<div id='msg'>תודה על פנייתך</div>");
            $("div#msg").fadeIn(6000, msgOut);
            //alert("תודה על פנייתך")
        }
    }
}
function msgOut() {
    $("div#msg").fadeOut(6000);
}
