//	באר עליון של הרשמה
var nextPage, nextTitle;
var pages = new Array (0,1,2,3);
var Titles = new Array ('פרטים אישיים', 'מי אני', 'אוהבת', 'לא אוהבת');
function iFrameRegEdit(c,x)
{
	if (!isNaN (c))
	{
		if (c < 3)
		{
			nextPage = pages[c+1];
			nextTitle = Titles[c+1];
			document.getElementById ('continueBtn').style.visibility = 'visible';
		}
		else
		{
			nextPage = pages[0];
			nextTitle = Titles[0];
			document.getElementById ('continueBtn').style.visibility = 'hidden';
		}
		if (c > 0)
		{
			prevPage = pages[c-1];
			prevTitle = Titles[c-1];
			document.getElementById ('prevBtn').style.visibility = 'visible';
		}
		else
		{
			prevPage = pages[3];
			prevTitle = Titles[3];
			document.getElementById ('prevBtn').style.visibility = 'hidden';
		}
	}
	else
	{
		return (iFrameRegEdit(pages[1], Titles[1]));
	}
	for (var ii = 0; ii<4; ii++) {
		//alert("iFrameReg_" + ii);
		document.getElementById("iFrameReg_" + ii).style.display='none';
		document.getElementById('titleImage_' + ii).src = (document.getElementById('titleImage_' + ii).src).replace('_on','_off');
	}
	for (var ii = 0; ii<4; ii++) {
		document.getElementById("class_" + ii).className='topMenuReg';
	}

	//	פתיחת עריכת הפריט
	document.getElementById("iFrameReg_" + c).style.display='';
	document.getElementById("class_" + c).className='topMenuRegHover';
	
	/*parent.document.getElementById('titleRegister').textContent=x;	// FireFox
	parent.document.getElementById('titleRegister').innerText=x;	// Explorer*/
	document.getElementById('titleImage_' + c).src = (document.getElementById('titleImage_' + c).src).replace('_off','_on');
}

//	וולידציה של טופס ההרשמה
function submitIt(form)
{
	mandatory = new Array (2);
	mandatory[0] = new Array (form.fName, form.lName, form.nickName, form.email, form.pass1, form.birthDay, form.birthMonth, form.birthYear, form.areaResidence, form.city, form.addrStreet, form.addrNum);
	mandatory[1] = new Array ('שם פרטי', 'שם משפחה', 'כינוי באתר', 'אימייל', 'סיסמא', 'יום בתאריך לידה', 'חודש בתאריך לידה', 'שנה בתאריך לידה', 'איזור מגורים', 'עיר', 'רחוב', 'מספר');
	var field1, fieldName;
	for (fld in mandatory[0])
	{
		field1 = mandatory[0][fld];
		fieldName = mandatory[1][fld];
		if (field1.value=="")
		{
			iFrameRegEdit(0);
			alert ("נא למלא " + fieldName);
			field1.focus();
			return false;
		}
	}
	
	/*
	if ((form.nickName.value==""))
	{
		iFrameRegEdit(0);
		alert ("נא למלא כינוי באתר")
		form.nickName.focus()
		return false;
	}
	if ((form.email.value==""))
	{
		iFrameRegEdit(0);
		alert ("נא למלא אימייל")
		form.email.focus()
		return false;
	}
	if ((form.pass1.value==""))
	{
		iFrameRegEdit(0);
		alert ("נא למלא סיסמא")
		form.pass1.focus()
		return false;
	}
	if ((form.birthDay.value==""))
	{
		iFrameRegEdit(0);
		alert ("נא למלא יום בתאריך לידה")
		form.birthDay.focus()
		return false;
	}
	
	if ((form.birthMonth.value==""))
	{
		iFrameRegEdit(0);
		alert ("נא למלא חודש בתאריך לידה")
		form.birthMonth.focus()
		return false;
	}
	
	if ((form.birthYear.value==""))
	{
		iFrameRegEdit(0);
		alert ("נא למלא שנה בתאריך לידה")
		form.birthYear.focus()
		return false;
	}
	
	if ((form.areaResidence.value==""))
	{
		iFrameRegEdit(0);
		alert ("בחרי איזור מגורים")
		form.areaResidence.focus()
		return false;
	}
	
	*/
	
	validate = new Array (form.fName, form.lName);
	for (fld in validate)
	{
		if (!IsNOTNumeric(validate[fld].value))
		{
			iFrameRegEdit(0);
			alert ("נא לכתוב אותיות בלבד")
			validate[fld].focus()
			return false;
		}
	}
	
	validate = new Array (form.city);
	for (fld in validate)
	{
		if (!IsHebrew(validate[fld].value))
		{
			iFrameRegEdit(0);
			alert ("נא לכתוב אותיות בעברית בלבד")
			validate[fld].focus()
			return false;
		}
	}

	if (form.emailverify.value != form.email.value)
	{
		iFrameRegEdit(0);
		alert ("נא לאמת את כתובת הדואר האלקטרוני")
		form.addrStreet.focus()
		return false;
	}

	if (!IsHebNumSign(form.addrStreet.value))
	{
		iFrameRegEdit(0);
		alert ("נא למלא כתובת חוקית")
		form.addrStreet.focus()
		return false;
	}
	
	if (!emailCheck(form.email.value))
	{
		iFrameRegEdit(0);
		alert("כתובת הדואר האלקטרוני שגויה")
		form.email.focus()
		return false
	}
	
	if ((form.pass1.value!=form.pass2.value))
	{
		iFrameRegEdit(0);
		alert ("סיסמא אינה תואמת")
		form.pass2.focus()
		return false;
	}
	
	//if ((form.cellolar.value==""))
	//{
	//	iFrameRegEdit(0);
	//	alert ("נא למלא סלולרי")
	//	form.cellolar.focus()
	//	return false;
	//}
	
	//str=form.cellolar.value
	//if ((str.length>11))
	//{
	//	iFrameRegEdit(0);
	//	alert ("נא למלא סלולרי תקין")
	//	form.cellolar.focus()
	//	return false;
	//}
	
	if (!IsNumDigits(form.phone.value, 9, 11))
	{
		iFrameRegEdit(0);
		alert ("טלפון לא נכון")
		form.phone.focus()
		return false;
	}
	
	if (!IsNumDigits(form.cellolar.value, 10, 11))
	{
		iFrameRegEdit(0);
		alert ("טלפון לא נכון")
		form.cellolar.focus()
		return false;
	}
	
	if (!IsNumDigits(form.addrZip.value, 5, 5) || !IsNumeric(form.addrZip.value))
	{
		iFrameRegEdit(0);
		alert ("נא לכתוב מיקוד חוקי")
		form.addrZip.focus()
		return false;
	}

    if ((form.terms.checked==false))
	{
		iFrameRegEdit(0);
		alert ("נא לאשר תקנון")
		return false;
	}
	
	if ((form.parents.checked==false))
	{
		iFrameRegEdit(0);
		alert ("נא לאשר אישור הורים")
		return false;
	}
}

//	וולידציה של אימייל
function validEmail(Email) {
	
		invalidChars = " /:,;"
		if (Email == "") {
		//alert("כתובת הדואר האלקטרוני אינה תקינה - הזן שנית")
		//form.c_mail.focus()
		return false
		}
		for (i=0; i<invalidChars.length; i++) {
			badChar = invalidChars.charAt(i)
			if (Email.indexOf(badChar,0) != -1) {
			     //alert("כתובת הדואר האלקטרוני אינה תקינה - הזן שנית")
				//form.c_mail.focus()
				return false
			}
		}
		atPos = Email.indexOf("@",1)
		if (atPos == -1) {
		//alert("כתובת הדואר האלקטרוני אינה תקינה - הזן שנית")
		//document.form.c_mail.focus()
			return false
		}
		if (Email.indexOf("@",atPos+1) != -1) {
		//alert("כתובת הדואר האלקטרוני אינה תקינה - הזן שנית")
		//document.form.c_mail.focus()
			return false
		}
		periodPos = Email.indexOf(".",atPos)
		if (periodPos == -1) {
		//alert("כתובת הדואר האלקטרוני אינה תקינה - הזן שנית")
		//document.form.c_mail.focus()
			return false
		}
		if (periodPos+3 > Email.length)	{
		//alert("כתובת הדואר האלקטרוני אינה תקינה - הזן שנית")
		//document.form.c_mail.focus()
			return false
		}
   return true
 }
 
 function IsNumeric(sText)
{
   var ValidChars = "-0123456789";
   var IsNumber=true;
   var Char;

   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
}

function IsNumDigits(sText, minDigits, maxDigits)
{
	var ValidChars = "-0123456789";
	var IsNumDigits=true;
	var Char;
	if (sText.length == 0)
	{
		return true;
	}
	for (i = 0; i < sText.length && IsNumDigits == true; i++) 
	{ 
		Char = sText.charAt(i); 
		if (ValidChars.indexOf(Char) == -1) 
		{
			IsNumDigits = false;
		}
	}
	if (sText.length < minDigits || sText.length > maxDigits)
	{
		IsNumDigits = false;
	}
	return IsNumDigits;
}

function IsNOTNumeric(sText)
{
   var ValidChars = "abcdefghijklmnopqrstuvwxyzןםאבגדהוזחטיכלמנסעפצקרשתץףךABCDEFGHIJKLMNOPQRSTUVWXYZ\'\`\\\/\.\' \"\:\;\(\)";
   var IsNumber=true;
   var Char;

   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
}

function IsHebrew(sText)
{
   var ValidChars = "ןםאבגדהוזחטיכלמנסעפצקרשתץףך\'\`\"-_ ";
   var IsHebrew=true;
   var Char;

   for (i = 0; i < sText.length && IsHebrew == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsHebrew = false;
         }
      }
   return IsHebrew;
}

function IsHebNumSign(sText)
{
   var ValidChars = "ןםאבגדהוזחטיכלמנסעפצקרשתץףך\\\/\.\' \"\:\;\(\)0123456789";
   var IsValid=true;
   var Char;

   for (i = 0; i < sText.length && IsValid == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsValid = false;
         }
      }
   return IsValid;
}

function IsEmail(sText)
{
   var ValidChars = "abcdefghijklmnopqrstuvwxyz\.\@_-";
   var IsNumber=true;
   var Char;

   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
}

function emailCheck (emailStr) {
/* The following pattern is used to check if the entered e-mail address
   fits the user@domain format.  It also is used to separate the username
   from the domain. */
var emailPat=/^(.+)@(.+)$/
/* The following string represents the pattern for matching all special
   characters.  We don't want to allow special characters in the address. 
   These characters include ( ) < @ , ; : \ " . [ ]    */
var specialChars="ןםאבגדהוזחטיכלמנסעפצקרשתץףך\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
/* The following string represents the range of characters allowed in a 
   username or domainname.  It really states which chars aren't allowed. */
var validChars="\[^\\s" + specialChars + "\]"
/* The following pattern applies if the "user" is a quoted string (in
   which case, there are no rules about which characters are allowed
   and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
   is a legal e-mail address. */
var quotedUser="(\"[^\"]*\")"
/* The following pattern applies for domains that are IP addresses,
   rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
   e-mail address. NOTE: The square brackets are required. */
var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
/* The following string represents an atom (basically a series of
   non-special characters.) */
var atom=validChars + '+'
/* The following string represents one word in the typical username.
   For example, in john.doe@somewhere.com, john and doe are words.
   Basically, a word is either an atom or quoted string. */
var word="(" + atom + "|" + quotedUser + ")"
// The following pattern describes the structure of the user
var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
/* The following pattern describes the structure of a normal symbolic
   domain, as opposed to ipDomainPat, shown above. */
var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")


/* Finally, let's start trying to figure out if the supplied address is
   valid. */

/* Begin with the coarse pattern to simply break up user@domain into
   different pieces that are easy to analyze. */
var matchArray=emailStr.match(emailPat)
if (matchArray==null) {
  /* Too many/few @'s or something; basically, this address doesn't
     even fit the general mould of a valid e-mail address. */
	//alert("Email address seems incorrect (check @ and .'s)")
	return false
}
var user=matchArray[1]
var domain=matchArray[2]

// See if "user" is valid 
if (user.match(userPat)==null) {
    // user is not valid
    //alert("The part of your email address before the '@' doesn't seem to be valid.")
    return false
}

/* if the e-mail address is at an IP address (as opposed to a symbolic
   host name) make sure the IP address is valid. */
var IPArray=domain.match(ipDomainPat)
if (IPArray!=null) {
    // this is an IP address
	  for (var i=1;i<=4;i++) {
	    if (IPArray[i]>255) {
	        //alert("Destination IP address is invalid!")
		return false
	    }
    }
    return true
}

// Domain is symbolic name
var domainArray=domain.match(domainPat)
if (domainArray==null) {
	//alert("Part of your email address after the '@' doesn't seem to be valid")
    return false
}

/* domain name seems valid, but now make sure that it ends in a
   three-letter word (like com, edu, gov) or a two-letter word,
   representing country (uk, nl), and that there's a hostname preceding 
   the domain or country. */

/* Now we need to break up the domain to get a count of how many atoms
   it consists of. */
var atomPat=new RegExp(atom,"g")
var domArr=domain.match(atomPat)
var len=domArr.length
if (domArr[domArr.length-1].length<2 || 
    domArr[domArr.length-1].length>6) {
   // the address must end in a two letter or other TLD including museum
   //alert("The address must end in a top level domain (e.g. .com), or two letter country.")
   return false
}

// Make sure there's a host name preceding the domain.
if (len<2) {
   var errStr="This address is missing a hostname!"
   //alert(errStr)
   return false
}

// If we've got this far, everything's valid!
return true;
}

function textCounter(field, countfield, maxlimit) {
if (field.value.length > maxlimit) // if too long...trim it!
field.value = field.value.substring(0, maxlimit);
// otherwise, update 'characters left' counter
else 
countfield.value = maxlimit - field.value.length;
}

