﻿var ie6 = isIE6(); 
var ie7 = isIE7(); 
var coords_array = new Array();
coords_array["student"] = [987, 901, 997, 1000];
coords_array["business"] = [982, 975, 997, 1000];
coords_array["leisure"] = [932, 925, 997, 1000];
var tab2Flag = false;
var tab2FlagClicked = false;
$(document).ready(function () {
    if (location.href.indexOf("mode=edit") != -1) {
        $("#content-2").show();
    }
    if (location.href.indexOf("#c-2") != -1) {
        $("#tab-1").removeClass("on");
        $("#tab-2").addClass("on");
        $("#content-1").hide();
        $("#content-2").show();
        tab2Flag = true;
        tab2FlagClicked = true;
    }
    $("#tab-2").click(function () {
        $("#tab-1").removeClass("on");
        $(this).addClass("on");
        $("#content-1").hide();
        $("#content-2").show();
        tab2FlagClicked = true;
    });
    $("#tab-1").click(function () {
        $("#tab-2").removeClass("on");
        $(this).addClass("on");
        $("#content-2").hide();
        $("#content-1").show();
        tab2FlagClicked = false;
    });
    $(".lang-splash").click(function (e) {
        array = mousePageXY(e);
        $("#lang-panel").css("margin-left", (array.x - 157) + "px");
        ShowOverlay();
        $("#lang-panel").show();
    });
    if (ie7) {
        $(".bottom-part .sub a span.f").each(function () {
            tmp = parseInt($(this).width()) + 34;
            $(this).next().css("width", tmp + "px");
        });
    }
    correctWidth();
    if (typeof addEventTo != "undefined")
        addEventTo(window, "resize", correctWidth);
    $("#tab-result").tabs();
    $("#accordion").accordion();
    $("#change_subscription_acc").css("height", "119px");
    $("input.big, textarea.big").focus(function () {
        $(this).addClass("focus");
    }).blur(function () {
        $(this).removeClass("focus");
    })

    /* Student, new */
    $(".double-link .dl-l .big-link").click(function () {
        dsp = $(".contact-info-box").css("display");
        if (dsp == "none") {
            $(".contact-info-box").slideDown();
            if (location.href.indexOf("company.aspx") == -1) {
                rr = $("#ctl00_cphLeft__educationItemControl_ctl00_ctl00_EducationId").val();
                Eduway.EducationActivityTrackingService.UpdateCounter(rr, "ContactShown");
            }
        } else {
            $(".contact-info-box").slideUp();
        }
    });
    $(".double-link .dl-r .big-link").click(function () {
        dsp = $(".qualification-info-box").css("display");
        if (dsp == "none") {
            $(".qualification-info-box").slideDown();
        } else {
            $(".qualification-info-box").slideUp();
        }
    });
    $("#mail-form-link").click(function () {
        dsp = $(".mail-form-box").css("display");
        if (dsp == "none") {
            $(".mail-form-box").slideDown();
            if (location.href.indexOf("company.aspx") == -1) {
                rr = $("#ctl00_cphLeft__educationItemControl_ctl00_ctl00_EducationId").val();
                Eduway.EducationActivityTrackingService.UpdateCounter(rr, "ApplicationFormShown");
            }
        } else {
            $(".mail-form-box").slideUp();
        }
    });
    //  Why do we clear values set by server?
    //  $(".form input[type=text],.form input[type=password], .form textarea").each(function(){$(this).val("");});
    $(".required, .phone, .email, .repeat, input.date, .digital").blur(function () {
        cl = $(this).attr("class");
        tmp = false;
        if (cl.indexOf("required") != -1) {
            tmp = valRequired($(this));
        } else if (cl.indexOf("phone") != -1) {
            tmp = valPhone($(this));
        } else if (cl.indexOf("email") != -1) {
            tmp = valEmail($(this));
        } else if (cl.indexOf("date") != -1) {
            tmp = valDate($(this));
        } else if (cl.indexOf("digital") != -1) {
            tmp = valDigital($(this));
        } else if (cl.indexOf("repeat") != -1) {
            tmp = valRepeat($(this));
        }
        if (tmp === true && $(this).attr("cid")) {
            $("input[repeatOf=" + $(this).attr("cid") + "]").each(function () {
                validateRepeat($(this));
            });
        }
        if (tmp === false) {
            $(this).parent().parent().find(".c-no .yes").hide();
            $(this).parent().parent().find(".c-no .no").show();
        } else {
            $(this).parent().parent().find(".c-no .no").hide();
            $(this).parent().parent().find(".c-no .yes").show();
        }
    });
    $(".phone").keyup(function (e) {
        var code = (e.keyCode ? e.keyCode : e.which);
        rr = new RegExp();
        rr = /48|49|50|51|52|53|54|55|56|57|58|59|107|109/;
        if (rr.test(code.toString()) == false) {
            tmp = $(this).val();
            reg = /^[0-9\-\(\)\+\s]+$/;
            if (reg.test(tmp)) return false;
            len = tmp.length - 1;
            $(this).val(tmp.substr(0, len));
        }
    });
    $(".mail-form-box .form .btn-small").click(function () {
        $(".required, .phone, .email, .repeat").each(function () {
            cl = $(this).attr("class");
            tmp = false;
            if (cl.indexOf("required") != -1) {
                tmp = valRequired($(this));
            } else if (cl.indexOf("phone") != -1) {
                tmp = valPhone($(this));
            } else if (cl.indexOf("email") != -1) {
                tmp = valEmail($(this));
            } else if (cl.indexOf("repeat") != -1) {
                tmp = valRepeat($(this));
            }
            if (tmp === true && $(this).attr("cid")) {
                $("input[repeatOf=" + $(this).attr("cid") + "]").each(function () {
                    validateRepeat($(this));
                });
            }
            if (!tmp) {
                $(this).parent().parent().find(".c-no .yes").hide();
                $(this).parent().parent().find(".c-no .no").show();
            } else {
                $(this).parent().parent().find(".c-no .no").hide();
                $(this).parent().parent().find(".c-no .yes").show();
            }
        });
        if ($(this).parent().parent().parent().find(".no:visible").length == 0) {
            $("#btn-small-shadow").css("display", "inline-block");
            $(".mail-form-box .btn-small").hide();
            var json = new Array();
            var pref = "#ctl00_cphLeft__educationItemControl_ctl00_ctl00_";
            json.push('"FirstName":"' + $(pref + "inpName").val() + '"');
            json.push('"LastName":"' + $(pref + "inpSurname").val() + '"');
            json.push('"Email":"' + $(pref + "inpEmail").val() + '"');
            json.push('"Phone":"' + $(pref + "inpPhone").val() + '"');
            json.push('"Comment":"' + $(pref + "inpMessage").val() + '"');
            json.push('"EducationId":"' + $(pref + "EducationId").val() + '"');
            if ($("input[type=checkbox]:checked").length == 1) {
                json.push('"Subscribe": true');
            } else {
                json.push('"Subscribe": "false"');
            }
            json_res = '{' + json.join(',') + '}';

            ApplicationFormDataSender.SendData(eval("(" + json_res + ")"), function (data) {
                if (data == true) {
                    $(".mail-form-box .form").hide();
                    $(".confirm").show();
                    $("#inpName, #inpSurname, #inpEmail, #inpPhone, #inpMessage").val("");
                    $("input[type=checkbox]:checked").each(function () { this.checked = false; });
                    $(".mail-form-box .c-no .no, .mail-form-box .c-no .yes").hide();
                    $("#btn-small-shadow").hide();
                    $(".mail-form-box .btn-small").css("display", "inline-block");
                    rr = $(pref + "EducationId").val();
                    Eduway.EducationActivityTrackingService.UpdateCounter(rr, "ApplicationFormSent");
                    setTimeout("$('.mail-form-box .form').show()", 2000);

                }
            }, function (data) {
                $("#btn-small-shadow").hide();
                $(".mail-form-box .btn-small").css("display", "inline-block");
                alert("Error, sorry...");
            });
        }
    });
    $('textarea.big').autoResize({
        extraSpace: 20
    });

    /* Profile page */
    $(".profiling .btn-small").click(function () {
        $("#acc_profile_fields .digital, #acc_profile_fields .phone, #acc_profile_fields .required, #acc_profile_fields .date, #acc_profile_fields .repeat").each(function () {
            cl = $(this).attr("class");
            tmp = false;
            if (cl.indexOf("required") != -1) {
                tmp = valRequired($(this));
            } else if (cl.indexOf("date") != -1) {
                tmp = valDate($(this));
            } else if (cl.indexOf("digital") != -1) {
                tmp = valDigital($(this));
            } else if (cl.indexOf("phone") != -1) {
                tmp = valPhone($(this));
            } else if (cl.indexOf("repeat") != -1) {
                tmp = valRepeat($(this));
            }
            if (tmp === true && $(this).attr("cid")) {
                $("input[repeatOf=" + $(this).attr("cid") + "]").each(function () {
                    validateRepeat($(this));
                });
            }
            if (!tmp) {
                $(this).parent().parent().find(".c-no .yes").hide();
                $(this).parent().parent().find(".c-no .no").show();
            } else {
                $(this).parent().parent().find(".c-no .no").hide();
                $(this).parent().parent().find(".c-no .yes").show();
            }
        });
        if ($("#acc_profile_fields .form").find(".no:visible").length != 0) {
            return false;
        }
    });
    $(".panel-form .btn-small").click(function () {
        $(".panel-form .required, .panel-form .email, .panel-form .repeat").each(function () {
            cl = $(this).attr("class");
            tmp = false;
            if (cl.indexOf("required") != -1) {
                tmp = valRequired($(this));
            } else if (cl.indexOf("email") != -1) {
                tmp = valEmail($(this));
            } else if (cl.indexOf("repeat") != -1) {
                tmp = valRepeat($(this));
            }
            if (tmp === true && $(this).attr("cid")) {
                $("input[repeatOf=" + $(this).attr("cid") + "]").each(function () {
                    validateRepeat($(this));
                });
            }
            if (!tmp) {
                $(this).parent().parent().find(".c-no .yes").hide();
                $(this).parent().parent().find(".c-no .no").show();
            } else {
                $(this).parent().parent().find(".c-no .no").hide();
                $(this).parent().parent().find(".c-no .yes").show();
            }
        });
        if ($(".panel-form").find(".no:visible").length != 0) {
            return false;
        }
    });
    $("#login_box_fields .btn_login").click(function () {
        $("#login_box_fields .required").each(function () {
            cl = $(this).attr("class");
            tmp = false;
            if (cl.indexOf("required") != -1) {
                tmp = valRequired($(this));
            } else if (cl.indexOf("email") != -1) {
                tmp = valEmail($(this));
            }
            if (!tmp) {
                $(this).parent().parent().find(".c-no .yes").hide();
                $(this).parent().parent().find(".c-no .no").show();
            } else {
                $(this).parent().parent().find(".c-no .no").hide();
                $(this).parent().parent().find(".c-no .yes").show();
            }
        });
        if ($("#login_box_fields .form").find(".no:visible").length != 0) {
            return false;
        }
    });
    $("a.remove").click(function () {
        f = confirm("Are you sure?");
        if (!f) return false;
        OnRemoveSubscription($(this).attr("id"));
        $(this).parent().parent().remove();
        g = parseInt($("#list_added_services").css("height")) - 22;
        $("#list_added_services").css("height", g + "px");
        len = $(".list-services table tr").length;
        if (len == 2) {
            $(".list-services table tr.dn").show();
        }
    });
    /* Popup showing for adding subscriptions  */
    $(".add-btn-box .add").click(function () {
        ShowDivAsPopup("popup_add_subsriptions");
    });
    /* top menu */
    $("#top_btn_login").click(function () {
        ShowDivAsPopup("popup_login");
    });
    $("input.date").click(function () {
        $("#dds").show();
    });
    slider();
});

function addSubjectSubscription(add_type, add_cat, add_id, add_subj) {
    // Next 3 lines must be replaced with really data.
    tmp = "<tr><td class='corn-l'></td><td class='close-col'><a class='remove' id='";
    tmp += add_id + "'>"
    tmp += "<img alt='x' src='http://uk.eduway.ws2.obiqsoft.se/App_Themes/student/img/none.gif'></a></td>";
    tmp += "<td class='col-1'><span>" + add_type + "</span></td>";
    tmp += "<td class='col-2'><span>" + add_cat + "</span></td>";
    tmp += "<td class='col-3'><span>" + add_subj + "</span></td><td class='corn-r'></td></tr>";

    $(".list-services table").append(tmp);
    $('.list-services table tr:last a.remove').bind('click', function () {
        f = confirm("Are you sure?");
        if (!f) return false;
        OnRemoveSubscription($(this).attr("id"));
        $(this).parent().parent().remove();
        g = parseInt($("#list_added_services").css("height")) - 22;
        $("#list_added_services").css("height", g + "px");
        len = $(".list-services table tr").length;
        if (len == 2) {
            $(".list-services table tr.dn").show();
        }
    });

    len = $(".list-services table tr").length;
    if (len == 2) {
        $(".list-services table tr.dn").show();
    } else {
        $(".list-services table tr.dn").hide();
    }
    g = parseInt($("#list_added_services").css("height")) + 22;
    $("#list_added_services").css("height", g + "px");
}
var count = 1;
var c_all = 0;
var count_banner = 1;
var c_all_banner = 0;
function slider() {
    li_list = $(".text-info table td").find("div");
    len = li_list.length;
    if (len < 2) return false;
    c_all = len;
    cc = 1;
    $(".text-info table td").find("div").each(function() {$(this).css("display", "none");});
    $(".text-info table td").find("div:first").css("display", "list-item");
    count = 2;
    setInterval('showNewText()', 6000);
}
function showNewText() {
    cc = 1;
    $(".text-info table td").find("div").each(function() {
        sp = $(this).find("span");
        if (sp.length > 0) {
            $(this).find("span").each(function(){$(this).hide();});
            if (cc == count) {
                $(this).show("slide", { direction: "right" }, 1000);
                intr = 500;
                ss = $(this).find("span:first");
                setTimeout("$(ss).show()",10);
                setTimeout("$(ss).next().show()",1100);
                setTimeout("$(ss).next().next().show()",1800);
                setTimeout("$(ss).next().next().next().show()",2600); 
            } 
            else 
                $(this).css("display", "none");
            cc++;
        } else {
            if (cc == count) {$(this).show("slide", { direction: "right" }, 1000); } else $(this).css("display", "none");
            cc++;
        }
    });
    if (count > c_all) {
        count = 2;
        $(".text-info table td").find("div:last").css("display", "none");
        $(".text-info table td").find("div:first").show("slide", { direction: "right" }, 1000);
    }
    else count += 1;
}
/* Получаем координаты курсора */
function mousePageXY(e)
{
	var x = 0, y = 0;
	if (!e) e = window.event;
	if (e.pageX || e.pageY) { x = e.pageX; y = e.pageY; }
	else if (e.clientX || e.clientY)
	{
		x = e.clientX + (document.documentElement.scrollLeft || document.body.scrollLeft) - document.documentElement.clientLeft;
		y = e.clientY + (document.documentElement.scrollTop || document.body.scrollTop) - document.documentElement.clientTop;
	}
	return {"x": parseInt(x), "y": parseInt(y)};
}
function isIE6() {
   if (navigator.userAgent.toString().indexOf("MSIE 6.0") != -1) return true;
   return false;
}
function isIE7() {
   if (navigator.userAgent.toString().indexOf("MSIE 7.0") != -1) return true;
   return false;
}
function correctWidth() {
   if (!type_page) type_page = "business";
   w = getClientWidth();
   if (parseInt(w) <= 1007) {
    if (!ie7&&!ie6) {
        $(".header .top-part .sub").css("width", coords_array[type_page][0] + "px");
    } else {
        $(".header .top-part .sub").css("width", coords_array[type_page][1] + "px");
        $(".header .menu").css("width", "879px");
    }
   } else {
    if (!ie7&&!ie6) {
        $(".header .top-part .sub").css("width", coords_array[type_page][2] + "px");
    } else {
        $(".header .top-part .sub").css("width", coords_array[type_page][3] + "px");
        $(".header .menu").css("width", "982px");
    }
   }
}
function getClientWidth()
{
  return document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientWidth:document.body.clientWidth;
}

function valPhone(obj) {
    if (!obj) return false;
    val = $(obj).val();
    reg = /^(\+)?[0-9\-\(\)\s]+$/;
    if (reg.test(val)) return true;
    return false;
}
function valRequired(obj) {
    if (!obj) return false;
    val = $(obj).val();
    reg = /^(\s)*[0-9a-zA-ZåÅöÖäÄüÜßа-яА-ЯÆæØø\-_\.,\:;\/@]+((\s)*[0-9a-zA-ZåÅöÖäÄüÜßа-яА-ЯÆæØø\-_\.,\:;\/@]+)*$/;
    if (reg.test(val)) return true;
    return false;
}
function valEmail(obj) {
    if (!obj) return false;
    val = $(obj).val();
    reg = /^([a-z0-9_]+\.)*[a-z0-9_]+@([a-z0-9_]+\.)+[a-z]{2,6}$/;
    if (reg.test(val.toLowerCase())) return true;
    return false;
}
function valDate(obj) {
    if (!obj) return false;
    val = $(obj).val();
    reg = /^[0-9\-]+$/;
    if (reg.test(val)) return true;
    return false;
}
function valDigital(obj) {
    if (!obj) return false;
    val = $(obj).val();
    reg = /^[0-9]+((\s)*[0-9]+)*$/;
    if (reg.test(val)) return true;
    return false;
}
function valRepeat(obj) {
    if (!obj) return undefined;
    var repeatOf = $("input[cid=" + $(obj).attr("repeatOf") + "]").val();
    var val = $(obj).val();
    if (repeatOf == val) return true;
    return false;
}
function validateRepeat(item) {
    var repeatOf = $("input[cid=" + item.attr("repeatOf") + "]").val();
    var val = item.val();
    var isValid = repeatOf == val;
    if (repeatOf && !val) {
        isValid = undefined;
    }
    if (isValid === false) {
        item.parent().parent().find(".c-no .yes").hide();
        item.parent().parent().find(".c-no .no").show();
    }
    else if (isValid === true) {
        item.parent().parent().find(".c-no .no").hide();
        item.parent().parent().find(".c-no .yes").show();
    }
}
