﻿var siteTitle = "سيارات مكتوب";
var logicalPath = "http://cars.maktoob.com/";
function tabChange(prefix, num, cur) {
    for (var i = 1; i <= num; i++) {
        if ($$(prefix + i))
            $$(prefix + i).className = "";
    }
    if ($$(prefix + cur))
        $$(prefix + cur).className = "on";
}
function tabs_global(tabPrefix, imgPfix, contPfix, num, cur) {
    tabChange(tabPrefix, num, cur);
    for (var i = 1; i <= num; i++) {
        var strimgPfix = imgPfix + i.toString();
        var strcontPfix = contPfix + i.toString();
        if (imgPfix != "")
            $$(strimgPfix).style.display = "none";
        if ($$(strcontPfix))
            $$(strcontPfix).style.display = "none";
    }
    strcontPfix = contPfix + cur.toString();
    if ($$(strcontPfix)) {
        $$(strcontPfix).style.display = "block";
    }
    return false;
}
function ValidateEmail(ID, Msgs) {
    var EmailString = $$(ID).value;
    var EmailFormat = new RegExp(/\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/);
    if (EmailFormat.exec(EmailString) == null) {
        $$(ID).focus();
        alert(Msgs);
        return false;
    }
    return true;
}
/*CHECK if the exceeded specific length or not*/
function isValidLongText(txtID) {
    var strComment = new String($$(txtID).value);
    if (strComment.split(" ").length == 1) {
        if (strComment.length < 55) return true;
        return false;
    }
    for (var i = 0; i < strComment.split(" ").length; i++) {
        if (strComment.split(" ")[i].length > 40) {
            return false;
        }
    }
    return true;
}
/*CLEAN SPECIAL CHARS*/
function cleanInjection(txtID) {
    if ($$(txtID).type != "file") {
        var re = new RegExp("[/\!\@\#$\%\^\&\*_+\}\|\(\)\{:\?\.\<\>\'\"]", "g");
        $$(txtID).value = $$(txtID).value.trim().replace(re, "");
    }
}
var ClipSearchPage = 'SearchMedia-Clip-[KEYWORD]-1.htm';
var ZoomSearchPage = 'SearchMedia-Zoom-[KEYWORD]-1.htm';
var NewsSearchPage = 'SearchNews-[KEYWORD]-1.htm';
var ArabySearch = 'WebSearch-[KEYWORD].htm';

function doSearch(txtID) {
    cleanInjection(txtID);
    if (!validateInput(txtID, 'الرجاء إدخال نص للبحث')) return false;
    switch (strSearchType) {
        case "Clip":
            self.location = ClipSearchPage.replace("[KEYWORD]", escape($$(txtID).value)); break;
        case "Zoom":
            self.location = ZoomSearchPage.replace("[KEYWORD]", escape($$(txtID).value)); break;
        case "News":
            self.location = NewsSearchPage.replace("[KEYWORD]", escape($$(txtID).value)); break;
        case "Internet":
            self.location = ArabySearch.replace("[KEYWORD]", escape($$(txtID).value)); break;
    }
    return false;
}
String.prototype.trim = function() {
    return this.replace(/^\s*/, "").replace(/\s*$/, "");
}
function validateInput(IDs, Msgs, btnID) {
    var strIDs = new String(IDs);
    var strMsgs = new String(Msgs);
    var ShowAlM = true;
    for (var i = 0; i < strIDs.split(",").length; i++) {
        var strCurrentID = strIDs.split(",")[i];
        var strCurrentMsg = strMsgs.split(",")[i];
        var isValid = true;
        switch ($$(strCurrentID).tagName.toLowerCase()) {
            case "select":
                if ($$(strCurrentID).value == -1)
                    isValid = false;
                break;

            case "input":
            case "textarea":
                if ($$(strCurrentID).type == "file") {
                    if ($$(strCurrentID).value != "" && $$(strCurrentID).value.toLowerCase().indexOf(".jpg") == -1) {
                        alert("(.Jpg) انواع الملفات المسموح بها فقط");
                        isValid = false;
                    }
                }
                if (strCurrentID.toLowerCase().indexOf("email") > -1) {
                    if (!ValidateEmail(strCurrentID, strCurrentMsg)) {
                        isValid = false;
                        ShowAlM = false;
                    }
                }
                else {
                    cleanInjection(strCurrentID);
                }

                if ($$(strCurrentID).type != "file")
                    if (!isValidLongText(strCurrentID)) {
                    alert("بعض الكلمات تحتوي على أحرف أكثر من المسموح");
                    $$(strCurrentID).select();
                    return false;
                }

                if ($$(strCurrentID).value == "")
                    isValid = false;
                break;
        }
        if (!isValid) {
            if (ShowAlM) {
                alert(strCurrentMsg);
            }
            $$(strCurrentID).focus();
            return false;
        }
    }
    if ($$("divLoadingUpld")) $$("divLoadingUpld").style.display = "block";
    if ($$(btnID)) $$(btnID).style.display = "none";
    if ($$("btnDum")) $$("btnDum").style.display = "block"; 
    return true;
}

var moveDownVarTimer;
function scrolling(contentID, ContainerID, Direction) {
    switch (Direction) {
        case "Right":
            if ($$(ContainerID).scrollLeft > 0) {
                clearTimeout(moveDownVarTimer);
                $$(ContainerID).scrollLeft -= 3;
                if (parseInt($$("imgScrl" + contentID.replace("di","")).alt, 10) > 0) {
                    $$("imgScrl" + contentID.replace("di", "")).alt = parseInt($$("imgScrl" + contentID.replace("di", "")).alt, 10) - 2;
                    $$("imgScrl" + contentID.replace("di", "")).style.left = $$("imgScrl" + contentID.replace("di", "")).alt + "px";
                }
            }
            else {
                return false;
                clearTimeout(moveDownVarTimer);
            }
            break;
        case "Left":
            if ($$(contentID).offsetWidth > $$(ContainerID).scrollLeft - $$(ContainerID).offsetWidth) {
                clearTimeout(moveDownVarTimer);
                $$(ContainerID).scrollLeft += 3;
                if (parseInt($$("imgScrl" + contentID.replace("di", "")).alt, 10) < 190) {
                    $$("imgScrl" + contentID.replace("di", "")).alt = parseInt($$("imgScrl" + contentID.replace("di", "")).alt, 10) + 2;
                    $$("imgScrl" + contentID.replace("di", "")).style.left = $$("imgScrl" + contentID.replace("di", "")).alt + "px";
                }
            }
            else {
                return false;
                clearTimeout(moveDownVarTimer);
            }
            break;
    }
    moveDownVarTimer = setTimeout("scrolling('" + contentID + "','" + ContainerID + "','" + Direction + "');", 10);
}
function stopscroll() { clearTimeout(moveDownVarTimer); }
function navTo(url) { self.location.href = url; }

function setCookie(c_name, value, expiredays) {
    var exdate = new Date();
    exdate.setDate(exdate.getDate() + expiredays);
    document.cookie = c_name + "=" + escape(value) + ";" + ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString());
}
function doVote(QID, box) {
     for (var i = 0; i < document.getElementsByName("rbVote" + QID).length; i++) {
        var tmpRdbtn = document.getElementsByName("rbVote" + QID).item(i);
        if (tmpRdbtn.checked) {
            setCookie("vote_" + QID, "1", 3);
            if (box == "Question")
                AJAXGet("Aspx_pages/Ajax_Submit.aspx?ajaxType=vote&AID=" + tmpRdbtn.value + "&r=" + parseInt(Math.random() * 100, 10), loadQuestion, "get");
            else
                AJAXGet("Aspx_pages/Ajax_Submit.aspx?ajaxType=vote&AID=" + tmpRdbtn.value + "&r=" + parseInt(Math.random() * 100, 10), loadVote, "get");
        }
    }
}
function CreatedFavoriteLink() {
    title = siteTitle;
    url = logicalPath;
    if (window.sidebar) {
        window.sidebar.addPanel(title, url, "");
    }
    else if (window.external) {
        window.external.AddFavorite(url, title);
    }
    else if (window.opera && window.print) {
        return true;
    }
}
