// הגדרות משתנים גלובליים
var rnd = new Date().toString().replace(/(:|\s)/gi,"");

// פונקצייה להמתנה
function WaitFor(millis){
    var date = new Date();
    var curDate = null;
    do { 
        curDate = new Date(); 
    }
    while(curDate-date < millis);
}

// יצירת מחרוזת אקראית
function GetRandomString(){
    var chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    var s = '';
    for (var i = 0; i < 32; i++) {
        var rnum = Math.floor(Math.random() * chars.length);
        s += chars.substring(rnum, rnum + 1);
    }
    return s;
}

// הוספת טאב בלחיצה על טאב
function tab() {if (event.keyCode==9) {document.selection.createRange().text="\t";return false;}}

// תיקון מסגרת בלחיצה על לינק
function fixUglyIE(){for (a in document.links) document.links[a].onfocus = document.links[a].blur;}
if (document.all){document.onmousedown = fixUglyIE;}


// ================================= Show Hide Elements ================================= //

function ShowHideDiv(obj){
	document.getElementById(obj).style.display=document.getElementById(obj).style.display=="block"?"none":"block";
}

function hidebnr(){
	CreateCookie('floatbnrclose',true,'')
	document.getElementById('floatbnr').style.display = "none";
}







// ======================================= Time and Date =================================== //

var dayarray=new Array("יום ראשון","יום שני","יום שלישי","יום רביעי","יום חמישי","יום שישי","שבת")
var montharray=new Array("ינואר","פברואר","מרץ","אפריל","מאי","יוני","יולי","אוגוסט","ספטמבר","אוקטובר","נובמבר","דצמבר")

function getthedate(){
var mydate=new Date()
var year=mydate.getYear()
if (year < 1000)
year+=1900
var day=mydate.getDay()
var month=mydate.getMonth()
var daym=mydate.getDate()
if (daym<10)
daym="0"+daym
var hours=mydate.getHours()
var minutes=mydate.getMinutes()
var seconds=mydate.getSeconds()
var dn="AM"
if (hours>=24)
dn="PM"
if (hours>24){
hours=hours-24
}
if (hours==0)
hours=24
if (minutes<=9)
minutes="0"+minutes
if (seconds<=9)
seconds="0"+seconds
var cdate="<span>"+dayarray[day]+", "+daym+" ב"+montharray[month]+", "+year+" &nbsp;|&nbsp; "+hours+":"+minutes+":"+seconds
+"</span>"
if (document.all)
document.all.clock.innerHTML=cdate
else if (document.getElementById)
document.getElementById("clock").innerHTML=cdate
else
document.write(cdate)
}
if (!document.all&&!document.getElementById)
getthedate()
function goforit(){
if (document.all||document.getElementById)
setInterval("getthedate()",1000)
}
// =================================== End Time and Date =================================== //



// קריאה לעוגייה - ויצירה אם לא קיימת
function GetCookie(name){
    if (ReadCookie(name)) {
        return ReadCookie(name);
    }
    var scookie = name
    CreateCookie(name,scookie,'Thu, 2 Aug 2050');
    rcookie = ReadCookie(name);
    if (scookie == rcookie) {
        return rcookie;
    }
    return '0';
}

// יצירת עוגייה
function CreateCookie(k,v,exp){
    cookieval = k + '=' + v + '; ' + ((exp)?('expires=' + exp + ' ;'):'') + 'path=/';
    document.cookie = cookieval;
}

// קריאה מעוגייה
function ReadCookie(k) {
    var theCookie=""+document.cookie;
    var ind=theCookie.indexOf(k);
    if (ind==-1 || k=="") return ""; 
    var ind1=theCookie.indexOf(';',ind);
    if (ind1==-1) ind1=theCookie.length; 
    return unescape(theCookie.substring(ind+k.length+1,ind1));
}

// מחיקת עוגייה
function eraseCookie(name){
	createCookie(name,"",-1);
}

// מחיקת רווח לפני ואחרי המחרוזת במידה ויש
function DoTrim(str){
	str = str.replace(/^\s*|\s*$/g,"");
	return str;
}

// קבלת הסיומת של הקובץ
function GetExtention(fileName){
	if(fileName != "")
		return fileName.substring(fileName.lastIndexOf(".")+1);
}

// העתקה להדבקה
function AddClip(holdtext){
	window.clipboardData.setData('Text', holdtext)
}

// Gets keycode.
function getKey(e){
	//var e = e?e:event;
	if (e == null) { // ie
		keycode = event.keyCode;
	} else { // mozilla
		keycode = e.which;
	}
	key = String.fromCharCode(keycode).toLowerCase();

//	alert(
//	"ctrl: " + event.ctrlKey + "\n" +
//	"Alt: "+ event.altKey + "\n" +
//	"Shift: "+ event.shiftKey + "\n" +
//	"Key: " + key
//	);
	return key;
}
//document.onkeypress = getKey;


//=========================== validate functions =======================//
function IsLength(inpt){
	inpt.value = DoTrim(inpt.value);
	if(inpt.value == ""){
		alert("שדה זה חובה");
		inpt.focus();
		return false;
	}
	else
		return true;
}
//=======================================//
function IsNumber(inpt){
	if(!IsLength(inpt))
		return false;
	
	if(isNaN(inpt.value)){
		alert("שדה זה חייב להכיל מספרים בלבד");
		inpt.focus();
		return false;
	}
	else
		return true;
}
//=======================================//
function IsEmail(inpt){
	if(!IsLength(inpt))
		return false;
	
	var email = inpt.value
	if((email.indexOf('@')>0)&&(email.indexOf('.')>0)&&(email.length > 5))
		return true;
	else
	{
		alert("שדה דואר אלקטרוני לא חוקי");
		inpt.focus();
		return false;
	}
}
//=======================================//
function IsAlpha(inpt){
	if(!IsLength(inpt))
		return false;
	
	var legalChar = "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz1234567890"
	
	if(!IsLegal(inpt.value, legalChar))	{
		alert("שדה זה חייב להכיל אותיות באנגלים ומספרים בלבד");
		inpt.focus();
		return false;
	}
	else
		return true;
}
//=======================================//
function IsEnglish(inpt){
	if(!IsLength(inpt))
		return false;
	
	var legalChar = "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz"
	
	if(!IsLegal(inpt.value, legalChar))	{
		alert("שדה זה חייב להכיל אותיות באנגלית בלבד");
		inpt.focus();
		return false;
	}
	else
		return true;
}
//=======================================//
function IsPositive(inpt){
	if(IsNumber(inpt))	{
		if(inpt.value < 0)		{
			alert("שדה זה חייב להיות חיובי");
			inpt.focus();
			return false;
		}
		else
			return true;
	}
}
//=======================================//
function IsNegative(inpt){
	if(IsNumber(inpt)){
		if(inpt.value >= 0){
			alert("שדה זה חייב להיות שלילי");
			inpt.focus();
			return false;
		}
		else
			return true;
	}
}
//=======================================//
function IsCompatible(inpt1, inpt2){
	if(inpt1.value != inpt2.value){
		alert("השדות אינם תואמים");
		inpt1.focus();
		return false;
	}
	else
		return true;
}
//=======================================//
function IsSelected(inpt){
	var tmpValue
	tmpValue = inpt.options[inpt.selectedIndex].value
	if(tmpValue == "" || tmpValue == "0"){
		alert("אנא בחר ערך");
		inpt.focus();
		return false;
	}
	else
		return true;
}
//=======================================//
function IsLegal(SourceString, legalChar)
{
	if(legalChar == "")
		legalChar = "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz1234567890"

	var IsGood = false;
	var strCheck = SourceString
	
	for(var i = 0; i < strCheck.length; i++){
		IsGood = false;
		for(var j = 0; j < legalChar.length; j++){
			if(legalChar.charAt(j) == strCheck.charAt(i)){
				IsGood = true;
				break;
			}
		}
		if(!IsGood){break;}
	}
	
	return IsGood;
}
//=======================================//
function IsMinLen(inpt, minNum){
	if(!IsLength(inpt))
		return false;
	
	if(inpt.value.length < minNum){
		alert("מינימום תווים בשדה זה: " + minNum);
		inpt.focus();
		return false;
	}
	else
		return true;
}
//======================= end validate functions =======================//
