﻿$(document).ready (init);

function init()
{
	if (($.browser.msie && jQuery.browser.version < 8)) {
		fixMinMaxwidth();
	};
	if ($("ul#newsList").length > 0) {
		function removeFirst() {
			first = $('ul#newsList li:first').html();
			$('ul#newsList li:first')
			.animate({ opacity: 0 }, speed)
			.slideUp('slow', function() { $(this).remove(); });
			addLast(first);
		}
		function addLast(first) {
			last = '<li style="display:none">' + first + '</li>';
			$('ul#newsList').append(last)
			$('ul#newsList li:last')
			.animate({ opacity: 1 }, speed)
			.fadeIn('slow')
		}    
		var first = 0;
		var speed = 1000;
		var pause = 9000;
		interval = setInterval(removeFirst, pause);
	};
    $("a.external").bind("click", function() {
        tempStr = this.href;
        window.open(tempStr, "", "");
        return false;
    });
    if ($("div#hp-wraper").length > 0) {
    	checkURI()
    }
    jQuery(function() {
    	jQuery('ul#nav').superfish();
    });
    if ($("input#subMainFrm").length > 0) {
    	$("form#mainFrm").bind("submit", function() {
    		checkFrm();
    		return false;
    	});
    };
    if ($("input#nl-sub").length > 0) {
    	$("form#frmNL").bind("submit", function() {
    		checkFrmNL();
    		return false;
    	});
    }
    if ($("input#sdn-sub").length > 0) {
    	$("form#frmSadna").bind("submit", function() {
    		checkFrmSDN();
    		return false;
    	});
    };
    
    if ($("div#sadnaForm h3").length > 0) {
    	$("div#sadnaForm h3").click(function() {
    		$("div#sadnaForm h3").toggleClass("open");
    		$("#frmSadna").slideToggle();
    	});
    };
    $("div#sadnaForm h3").toggleClass("open");
    $("#frmSadna").slideToggle();

    if ($("div.sadnaItem").length > 0) {
    	$("div.sadnaItem").hover(function() {
    		var position = $(this).offset();
    		var htm = $(this).children("div").html();
    		var dv;
    		if ($("html").css("direction") == "ltr") {
    			//dv = $("<div/>").attr("id", "tmpDiv").html(htm).addClass("quickView").css("right", position.left - 200).css("top", position.top);
    		}
    		else {
    			if (!($.browser.msie && jQuery.browser.version == 6)) {
    				dv = $("<div/>").attr("id", "tmpDiv").html(htm).addClass("quickView").css("left", position.left - 160).css("top", position.top);
    			}
    		}
    		$("body").prepend(dv);
    		//$(this).children("div").removeClass("hidden").addClass("quickView");
    	}
    	, function() {
    		//$(this).children("div").stop().addClass("hidden").removeClass("quickView");
    		$("div#tmpDiv").remove();
    	});
    };
    if (($.browser.msie && jQuery.browser.version == 6)) {
    	$('div#colRight, div#colCenter').supersleight({ shim: '/images/x.gif' });
    	//$(document).pngFix(); 
    };
}

$(window).bind("resize", function() {
	fixMinMaxwidth();
});

function checkURI() {
	if (document.location.search != "") {
		var tmp = document.location.search.toString();
		if (tmp.indexOf("suc=1") > 0) {
			$("#hp-wraper").append("<div id='msg'>תודה על פנייתך</div>");
			$("div#msg").fadeIn('10000', msgOut);
			//alert("תודה על פנייתך")
		}
	}
}
function msgOut() {
	$("div#msg").fadeOut(9000);
}

function checkFrm() {
	var el;
	el = $("#fullName")[0];
	if (isBlank(el.value)) {
		setError(el,"נא להזין שם מלא");
		return false;
	}
	else {
		clearError(el);
	}
	el = $("#bDate")[0];
	if (isBlank(el.value)) {
		setError(el,"נא להזין תאריך לידה");
		return;
	}
	else {
		clearError(el);
	}
	el = document.getElementsByName("sex");
	i = 0
	for(i=0; i<el.length; i++) {
		if (el[i].checked) {
			i = 1
		}
	}
	if (i = 0) {
		setErrorNoFocus(el, "נא לסמן מין");
		return;
	}
	else {
		clearError(el);
	}
	el = $("#email")[0];
	if (!checkEmail(el.value)) {
		setError(el,"נא להזין כתובת תקינה");
		return;
	}
	else {
		clearError(el);
	}
	el = $("#telStr")[0];
	elem = $("#cellStr")[0];
	if (isBlank(el.value) && isBlank(elem.value)) {
		setError(el,"נא להזין מספר טלפון או נייד");
		return;
	}
	else {
		clearError(el);
	}

	$('#mainFrm').submit()
	return false;
}

function checkFrmNL() {
	var el;
	el = $("#nl-fullName")[0];
	if (isBlank(el.value)) {
		setErrorNoMsg(el);
		return false;
	}
	else {
		clearError(el);
	}
	el = $("#nl-email")[0];
	if (!checkEmail(el.value)) {
		setErrorNoMsg(el);
		return;
	}
	else {
		clearError(el);
	}
	$('#frmNL').submit()
	return false;
}

function checkFrmSDN() {
	var el;
	el = $("#sdn-fullName")[0];
	if (isBlank(el.value)) {
		setErrorNoMsg(el);
		return false;
	}
	else {
		clearError(el);
	}
	el = $("#sdn-email")[0];
	if (!checkEmail(el.value)) {
		setErrorNoMsg(el);
		return;
	}
	else {
		clearError(el);
	}
	el = $("#sdn-telStr")[0];
	if (isBlank(el.value)) {
		setErrorNoMsg(el);
		return false;
	}
	else {
		clearError(el);
	}
	$('#frmSadna').submit()
	return false;
}

String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/gm, '');
}

function setErrorNoFocus(el,msg) {
	$(el).addClass("validateError");
	elem = $(el)[0].id.toString();
	$("#"+elem+"-div").text(msg);
}
function setError(el, msg) {
	$(el).addClass("validateError");
	elem = $(el)[0].id.toString();
	$("#" + elem + "-div").text(msg);
	el.focus();
}
function setErrorNoMsg(el) {
	$(el).addClass("validateError");
	el.focus();
}
function clearError(el) {
	$(el).removeClass("validateError");
	elem = $(el)[0].id.toString();
	$("#"+elem+"-div").text("");
}

//anonymous function to check all elements with class .fixMinMaxwidth
var fixMinMaxwidth = function() {
	//only apply this fix to browsers without native support
	if (typeof document.body.style.maxHeight !== "undefined" &&
               typeof document.body.style.minHeight !== "undefined") return false;

	//loop through all elements
	$('.fixMinMaxwidth').each(function() {
		//get max and minwidth via jquery
		var maxWidth = parseInt($(this).css("max-width"));
		var minWidth = parseInt($(this).css("min-width"));

		//if min-/maxwidth is set, apply the script
		if (maxWidth > 0 && $(this).width() > maxWidth) {
			$(this).width(maxWidth);
		} else if (minWidth > 0 && $(this).width() < minWidth) {
			$(this).width(minWidth);
		}
	});
}


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 z = 0; z < val.length; z++) {
        if (!isDigit(val.charAt(z))) { 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 = ""; } }
