var xmlRequest = new xmlObj(false);
/* ------------------------------------------------------------------------------------------------ */
/* submitLogInForm */
/* ------------------------------------------------------------------------------------------------ */
function submitLoginForm()
{
	var formValidator = new Validator('theForm');
	formValidator.addValidation('username', 'required', 'יש להזין שם משתמש');
	formValidator.addValidation('password', 'required', 'יש להזין סיסמא');
	if (formValidator.validate ())
	{
		return true;
	}

}
/* ------------------------------------------------------------------------------------------------ */
/* check date */
/* ------------------------------------------------------------------------------------------------ */
function isdate()
{
	var day, year, month;
	day=document.getElementById("form2_bday").value;
	month=document.getElementById("form2_bmonth").value;
	year=document.getElementById("form2_byear").value;
	if ((day!=00)&&(month!=00)&&(year!=0000))
	{
		if ((month==04)||(month==06)||(month==09)||(month==11))
		{
			if(day>30)
				{
					alert("בחודש "+month+" יש עד 30 ימים");
					return false;
				}
		}

		if(month==02)
		{
				if(day>28)
				{
					if (year % 4 != 0)
					{
						alert("בחודש "+month+" יש עד 28 ימים");
						return false;	
					}
				}
		}	
		
		if (year % 4 == 0)
		{
				if(month==02)
				{
					if(day>29)
					{
						alert("בחודש "+month+" יש עד 28 ימים");
						return false;	
					}
				}
		}
		document.getElementById("birthDate").value=year+"-"+month+"-"+day;
	}
	return true;
}

/* ------------------------------------------------------------------------------------------------ */
/* check password */
/* ------------------------------------------------------------------------------------------------ */
function checkGamerPassword () 	
{
	var password  = document.getElementById("registerPass").value;
	var cNewPassword = document.getElementById("passVerification").value;
	
		if (password != cNewPassword)
		{
			alert("הסיסמא שהזנת שנית לא נכונה");
			document.getElementById("passVerification").value = "";
			document.getElementById("passVerification").focus();
			return false;
		}
		if (isdate())
		{
			document.getElementById("formAddUsers").submit();
		}	
	return false;
}
/* ------------------------------------------------------------------------------------------------ */
/* submitSiteRegisterForm */
/* ------------------------------------------------------------------------------------------------ */
function submitMembersForm ()
{
	var formValidator = new Validator('formAddUsers');
	formValidator.addValidation('firstname', 'required', 'יש להזין שם פרטי');
	formValidator.addValidation('lastname', 'required', 'יש להזין שם משפחה');
	formValidator.addValidation('email', 'required', 'יש להזין כתובת אימייל');
	formValidator.addValidation('email', 'email', 'יש להזין אימייל חוקי');
	formValidator.addValidation('registerUsername', 'required', 'יש להזין שם משתמש');
	formValidator.addValidation('registerPass', 'required', 'יש להזין סיסמא');
	formValidator.addValidation('passVerification', 'required', 'יש להזין אימות סיסמא');
	formValidator.addValidation('registerPhone', 'phone', 'יש להזין טלפון חוקי');

	if (formValidator.validate ())
	{
		if (document.getElementById("mailingList8").checked == true)
		{
			document.getElementById("mailingList8").value = "on";
		}	
		checkGamerPassword ();		 
	}

	return false; 
}
/* ----------------------------------------------------------------------------------------------------------------------------	*/
/* isEnglish																										*/
/* ----------------------------------------------------------------------------------------------------------------------------	*/
function isEnglish(type)
{
	var nameENG=document.getElementById("nameENG").value;
	var charpos = nameENG.search("[^A-Za-z0-9 ]"); 
    if(nameENG.length > 0 &&  charpos >= 0) 
    { 
       	alert("שם " + type + " יכול להכיל רק אותיות באנגלית");
		document.getElementById("nameENG").focus();
		return false;
	}	
	return true;
}
/* ----------------------------------------------------------------------------------------------------------------------------	*/
/* isNumber																										*/
/* ----------------------------------------------------------------------------------------------------------------------------	*/
function isNumber(type)
{
	var value=document.getElementById(type).value;
	if (value != "")
	{
		var charpos = value.search("[^0-9]"); 
    	if(value.length > 0 &&  charpos >= 0) 
    	{ 
       		alert("שדה "+type+" יכול להכיל רק ספרות");
			document.getElementById(type).focus();
			return false;
		}
	}	
	return true;
}
/* ------------------------------------------------------------------------------------------------ */
/* submitProducerTicketForm */
/* ------------------------------------------------------------------------------------------------ */
function submitProducerTicketForm ()
{
	tinyMCE.triggerSave();
	
	var formValidator = new Validator('openTicket');
	var area = document.getElementById("area").value; 
	var subCat = document.getElementById("subCat").value; 
	formValidator.addValidation('cellPhone', 'phone', 'יש להזין מספר פלאפון חוקי');
	formValidator.addValidation('fax', 'phone', 'יש להזין מספר פקס חוקי');
	formValidator.addValidation('title', 'required', 'יש להזין כותרת');
	formValidator.addValidation('desc', 'required', 'יש להזין תוכן');
	
	if (formValidator.validate ())
	{
		document.getElementById("logoFile").value = document.getElementById("essayFileName_spn").innerHTML;
		if (document.getElementById("showEmail").checked == true)
		{
			document.getElementById("showMail").value = "1";
		}
		else
		{
			document.getElementById("showMail").value = "0";
		}
		if (area == "0")
		{
			alert("יש לבחור אזור");
			return;
		}
		if (subCat == "0")
		{
			alert("יש לבחור קטגוריה");
			return;
		}
		setFilesNames("openTicket");
		document.getElementById("openTicket").submit(); 
	}

	return false; 
}
/* ------------------------------------------------------------------------------------------------ */
/* submiAddEssay */
/* ------------------------------------------------------------------------------------------------ */
function submitAddEssay ()
{
	tinyMCE.triggerSave();
	
	var cat = document.getElementById("cat").value;
	if (cat == "0")
	{
		alert("יש לבחור קטגורית מאמר");
		return;
	}
	var formValidator = new Validator('addEssay');
	formValidator.addValidation('title', 'required', 'יש להזין כותרת');
	formValidator.addValidation('desc', 'required', 'יש להזין תוכן');
	formValidator.addValidation('subTitle', 'required', 'יש להזין תיאור קצר');

	if (formValidator.validate ())
	{
		document.getElementById("essayFile").value = document.getElementById("essayFileName_spn").innerHTML;
		document.getElementById("addEssay").submit(); 
	}

	return false; 
}
/* ------------------------------------------------------------------------------------------------ */
/* setFilesNames																					*/
/* ------------------------------------------------------------------------------------------------ */
function setFilesNames (formName)
{
	oForm = document.getElementById(formName);

	filesNamesStr = "";

	oTbl = document.getElementById("picsTbl").getElementsByTagName("tbody")[0];

	numPics = oTbl.rows.length;

	for (r=1; r<numPics; r++)
	{
		row = oTbl.rows[r];

		picName = row.cells[1].childNodes[1].id;

		if (filesNamesStr != "") filesNamesStr += " ";

		filesNamesStr += picName.replace(/\s/g, "_");
	}
	oForm.filesNames.value = filesNamesStr;
}
/* -------------------------------- */
/* addRating				    	*/
/* -------------------------------- */
function addRating (score,pageId)
{
  	xml = "<data>" +
			"<command>private.addRating</command>" +
				"<score>"+score+"</score>"	+
				"<pageId>"+pageId+"</pageId>"	+
		  "</data>";
	xmlRequest.init (xml);
	xmlRequest.sendAsyncRequest ("server.php", xmlRequest.obj, "afterAddRating");
}

/* ----------------------------------------------------------------------------------------------------------------------------	*/
/* afterAddRating																											*/
/* ----------------------------------------------------------------------------------------------------------------------------	*/
function afterAddRating ()
{
	xmlRequest.init(commonDecode(asyncHttpObj.responseText));

	try
	{
		var returnValue = xmlRequest.getValue("success");
		if (returnValue == 1)
		{		
			alert ('תודה, דרוגך התקבל בהצלחה');
			window.location.reload();
		}
		else
		{
			alert ('מותר לדרג את המאמר רק פעם אחת בלבד');
		}

			
	}
	catch (e)
	{
		alert ("AJAX Error");
	}
}
/* ----------------------------------------------------------------------------------------------------------------------------	*/
/* showAddFeedBack											    																*/
/* ----------------------------------------------------------------------------------------------------------------------------	*/
function showAddFeedBack()
{
	document.getElementById("addFeedBack").style.display="block";
}
/* ----------------------------------------------------------------------------------------------------------------------------	*/
/* closeWin											    																*/
/* ----------------------------------------------------------------------------------------------------------------------------	*/
function closeWin(name)
{
	document.getElementById(name).style.display="none";
	window.location.reload();
}
/* -------------------------------- */
/* addFeedBack				    	*/
/* -------------------------------- */
function addFeedBack ()
{
	var formValidator = new Validator('addFeedBackForm');
	formValidator.addValidation('writerName', 'required', 'יש להזין שם');
	formValidator.addValidation('mail', 'required', 'יש להזין דוא"ל');
	formValidator.addValidation('mail', 'email', 'יש להזין דוא"ל חוקי');
	formValidator.addValidation('title', 'required', 'יש להזין כותרת');
	formValidator.addValidation('content', 'required', 'יש להזין תוכן');

	if (formValidator.validate ())
	{
		var writerName = document.getElementById("writerName").value;
		var mail = document.getElementById("mail").value;
		var title = document.getElementById("title").value;
		var content = document.getElementById("content").value;
		var essayId = document.getElementById("essayIdF").value;
		

		xml = "<data>" +
				"<command>private.addFeedBack</command>" +
				"<writerName>"+writerName+"</writerName>"	+
				"<mail>"+mail+"</mail>"	+
				"<title>"+title+"</title>"	+
				"<content>"+content+"</content>" +
				"<essayId>"+essayId+"</essayId>" +
		 	 "</data>";
		xmlRequest.init (xml);
		xmlRequest.sendAsyncRequest ("server.php", xmlRequest.obj, "afterAddFeedBack");
	}
  
}

/* ----------------------------------------------------------------------------------------------------------------------------	*/
/* afterAddRating																											*/
/* ----------------------------------------------------------------------------------------------------------------------------	*/
function afterAddFeedBack ()
{
	xmlRequest.init(commonDecode(asyncHttpObj.responseText));

	try
	{
		var returnValue = xmlRequest.getValue("success");
		if (returnValue == 1)
		{		
			document.getElementById("addFeedBack").innerHTML="<div class='afterAddFeedBack'>" +
																"<div class='afterAddFeedBackTxt'>תגובתך התקבלה במערכת, המערכת תשקול אם לפרסמה באתר</div></div>" +
																 "<div class='closeWin'><img src='loadedFiles/closeWin.png' alt='סגור חלון' onclick=closeWin('addFeedBack') /></div>" +
															  "</div>";
		}
		else
		{
			alert ('מותר לדרג את המאמר רק פעם אחת בלבד');
			window.location.reload();
		}

			
	}
	catch (e)
	{
		alert ("AJAX Error");
	}
}
/* ----------------------------------------------------------------------------------------------------------------------------	*/
/* advSearch																						            					*/
/* ----------------------------------------------------------------------------------------------------------------------------	*/
function advSearch()
{
	var freeField = document.getElementById("freeSearch").value;
	var mainCat =  document.getElementById("mainCat").value;
	var area =  document.getElementById("area").value;
	var subCat =  document.getElementById("subCat").value;

	if (freeField == "" && mainCat == '0' && area == '0' && subCat == '0')
	{
		alert("יש לבחור להזין לפחות שדה אחד");
		return;
	}
	if (mainCat == '0')
	{
		alert("יש לבחור היכן לחפש");
		return;
	}
	document.getElementById("search").submit();
}
/* ----------------------------------------------------------------------------------------------------------------------------	*/
/* sendPass																						       					*/
/* ----------------------------------------------------------------------------------------------------------------------------	*/
function sendPass()
{
	var formValidator = new Validator('passForm');
	formValidator.addValidation('sendUsername', 'required', 'יש להזין שם משתמש');
	if (formValidator.validate ())
	{
		document.getElementById("passForm").submit();;
	}	
}
