
/* ----------------------------------------------------------------------------------------------------------------	*/
/* submitRegForm																									*/
/* ----------------------------------------------------------------------------------------------------------------	*/
function submitRegForm ()
{
	var formValidator = new Validator('registrationForm');
			
	formValidator.clearAllValidations ();
	formValidator.addValidation('firstname',		'required',		'יש להזין שם');
	formValidator.addValidation('lastname',			'required',		'יש להזין משפחה');
	formValidator.addValidation('username',			'required',		'יש להזין שם משתמש');
	formValidator.addValidation('password',			'required',		'יש להזין סיסמה');
	formValidator.addValidation('confirmPassword',	'required',		'יש להזין אימות סיסמה');
	formValidator.addValidation('email',			'required',		'יש להזין כתובת מייל');
	formValidator.addValidation('email',			'email',		'כתובת מייל לא חוקית');
	
	if (formValidator.validate ())
	{
		oForm	= document.getElementById ("registrationForm");

		if (oForm.password.value != oForm.confirmPassword.value)
		{
			alert ("אימות הסיסמא אינו תואם. יש להקלידו שוב");
			return false;
		}

		oForm.birthDate.value = oForm.birthDay.value + "-" + oForm.birthMonth.value + "-" + oForm.birthYear.value;

		return (checkDate ());
	}
	return false;
}

/* ----------------------------------------------------------------------------------------------------------------	*/
/* validate date																									*/
/* ----------------------------------------------------------------------------------------------------------------	*/
function checkDate ()
{
	oForm	= document.getElementById ("registrationForm");
	dtCh = "-";

	var daysInMonth = DaysArray(12)

	var strDay		= oForm.birthDay.value;
	var strMonth	= oForm.birthMonth.value;
	var strYr		= oForm.birthYear.value;

	if (strYr == "" && (strMonth != "" || strDay != ""))
	{
		alert ("יש לבחור שנה");
		return false;
	}

	if (strMonth == "" && (strYr != "" || strDay != ""))
	{
		alert ("יש לבחור חודש");
		return false;
	}

	if (strDay == "" && (strMonth != "" || strYr != ""))
	{
		alert ("יש לבחור יום");
		return false;
	}

	month	= strMonth*1;
	day		= strDay*1
	year	= strYr*1;

	if ((month==2 && day>daysInFebruary(year)) || day > daysInMonth[month])
	{
		alert ("תאריך לא קיים");
		return false;
	}

	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false)
	{
		alert ("תאריך לא חוקי");
		return false;
	}

	return true
}

function isInteger(s)
{
	var i;
    for (i = 0; i < s.length; i++)
	{   
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    return true;
}

function stripCharsInBag(s, bag)
{
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year)
{
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}

function DaysArray(n) 
{
	for (var i = 1; i <= n; i++) 
	{
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

/* ----------------------------------------------------------------------------------------------------------------	*/
/* submitLoginForm																									*/
/* ----------------------------------------------------------------------------------------------------------------	*/
function submitLoginForm()
{
	var formValidator = new Validator('loginForm');

	formValidator.clearAllValidations ();

	formValidator.addValidation('username',		'required',		'יש להזין שם משתמש');
	formValidator.addValidation('password',		'required',		'יש להזין סיסמה');

	return formValidator.validate ();
}
	
/* ----------------------------------------------------------------------------------------------------------------	*/
/* submitLessonForm																									*/
/* ----------------------------------------------------------------------------------------------------------------	*/
function submitLessonForm ()
{
	if (multiUploadCountFiles != 0)
	{
		alert ("יש להמתין לסיום טעינת הקבצים");
		return false;
	}

	tinyMCE.triggerSave();

	oForm = document.getElementById("lessonOutlineForm");

	var formValidator = new Validator("lessonOutlineForm");

	formValidator.clearAllValidations ();

	formValidator.addValidation("class",		"required",		"יש לבחור כיתה");
	formValidator.addValidation("metier",		"required",		"יש להזין מקצוע");
	formValidator.addValidation("subject",		"required",		"יש להזין נושא");
	formValidator.addValidation("title",		"required",		"יש להזין כותרת");
	formValidator.addValidation("lessonBody",	"required",		"יש להזין את גוף השיעור");

	if (formValidator.validate ())
	{
		if (!oForm.agree.checked)
		{
			alert ("יש לקרוא את תנאי שליחת תוכן לאתר ולאשר");
			return false;
		}

		setFilesNames ();
		return true;
	}
	
	return false;
}

/* ------------------------------------------------------------------------------------------------ */
/* setFilesNames																					*/
/* ------------------------------------------------------------------------------------------------ */
function setFilesNames ()
{
	oForm = document.getElementById("lessonOutlineForm");

	var filesNames = "";

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

	numFiles = oTbl.rows.length;

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

		picName   = row.cells[1].childNodes[0].id;	// innerText;

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

		filesNames += picName.replace(/\s/g, "_");
	}

	oForm.filesNames.value = filesNames;

}

var xmlRequest = new xmlObj(false);

/* ------------------------------------------------------------------------------------------------ */
/* showHideFeedbackForm																				*/
/* ------------------------------------------------------------------------------------------------ */
function showHideFeedbackForm ()
{
	if (document.getElementById("addLessonFeedback").style.display == "")
		document.getElementById("addLessonFeedback").style.display = "none";
	else
	{
		document.getElementById("downloadLessonBox").style.display = "none";
		document.getElementById("addLessonFeedback").style.display = "";
	}
}

/* ------------------------------------------------------------------------------------------------ */
/* submitAddLessonFeedback																			*/
/* ------------------------------------------------------------------------------------------------ */
function submitAddLessonFeedback()
{
	var formValidator = new Validator('addLessonFeedbackForm');
									
	formValidator.addValidation('name',		'required',		'יש להזין שם');
	formValidator.addValidation('email',	'required',		'יש להזין דוא\"ל');
	formValidator.addValidation('title',	'required',		'יש להזין כותרת');
	formValidator.addValidation('email',	'email',		'יש להזין דוא\"ל חוקי');
	
	if (formValidator.validate ())
	{
		oForm = document.getElementById("addLessonFeedbackForm");

		requestXml = "<data>"	+
						"<command>private.addLessonFeedback</command>" +
						"<lessonId>"  + oForm.lessonId.value  	+ "</lessonId>"  +
						"<name>"  	  + oForm.name.value  		+ "</name>"  	 +
						"<email>"  	  + oForm.email.value  		+ "</email>"  	 +
						"<title>"  	  + oForm.title.value  		+ "</title>"  	 +
						"<feedback>"  + oForm.feedback.value  	+ "</feedback>"  +
					 "</data>";

		xmlRequest.init (requestXml);
	
		xmlRequest.sendAsyncRequest ("server.php", xmlRequest.obj, "submitAddLessonFeedback_response");
	}

	return false;
}

function submitAddLessonFeedback_response ()
{
	document.getElementById("addLessonFeedback").style.display = "none";
	document.getElementById("addLessonFeedbackForm").reset ();

	alert ("תגובתך התקבלה בהצלחה.");

	window.location.reload();
}

/* ------------------------------------------------------------------------------------------------ */
/* oneFeedbackCollapse																				*/
/* ------------------------------------------------------------------------------------------------ */
function oneFeedbackCollapse (i)
{
	var oFeedback = document.getElementById("feedback"+i);

	if (oFeedback.style.display == "none")
	{
		oFeedback.style.display = "block";
		document.getElementById("feedbackTitle"+i).className = "feedback_title_open"
	}
	else
	{
		oFeedback.style.display = "none";
		document.getElementById("feedbackTitle"+i).className = "feedback_title"
	}

}

/* ------------------------------------------------------------------------------------------------ */
/* feedbacksCollapse																				*/
/* ------------------------------------------------------------------------------------------------ */
function feedbacksCollapse ()
{
	var i = 1;
	var oFeedback = document.getElementById("feedback"+i);

	while(oFeedback != null)
	{
		if (oFeedback.style.display == "none")
		{
			oFeedback.style.display = "block";

			if (oFeedback.innerHTML != "")
				document.getElementById("feedbackTitle"+i).className = "feedback_title_open"
		}
		else
		{
			oFeedback.style.display = "none";
			document.getElementById("feedbackTitle"+i).className = "feedback_title"
		}

		i++;
		oFeedback = document.getElementById("feedback"+i);
	}
}

/* ------------------------------------------------------------------------------------------------ */
/* rateLesson																						*/
/* ------------------------------------------------------------------------------------------------ */
function rateLesson (lessonId, rate)
{
	requestXml = "<data>"	+
					"<command>private.rateLesson</command>" +
					"<lessonId>"  + lessonId  	+ "</lessonId>"  +
					"<rate>"  	  + rate  		+ "</rate>"  	 +
				 "</data>";

	xmlRequest.init (requestXml);
	
	xmlRequest.sendAsyncRequest ("server.php", xmlRequest.obj, "rateLesson_response");
}

function rateLesson_response (i)
{
	xmlRequest.init(commonDecode(asyncHttpObj.responseText));

	try
	{
		var success  = xmlRequest.getValue("success");
	}
	catch (e)
	{
		alert ("AJAX Error");
		return false;
	}

	if (success == "0")
	{
		alert ("מותר לדרג כל שיעור רק פעם אחת בלבד");
	}
	else
	{
		alert ("תודה, דירוגך התקבל בהצלחה");
		window.location.reload();
	}
	
	return false;
}
	
/* ------------------------------------------------------------------------------------------------ */
/* printLesson																						*/
/* ------------------------------------------------------------------------------------------------ */
function printLesson (lessonId)
{
	window.open ("http://www.marpad.co.il/index2.php?id=54&lessonId=" + lessonId + "&lang=HEB");
}

/* ------------------------------------------------------------------------------------------------ */
/* downloadLesson																					*/
/* ------------------------------------------------------------------------------------------------ */
function downloadLesson ()
{
}

/* ------------------------------------------------------------------------------------------------ */
/* setAddAutoComplete																				*/
/* ------------------------------------------------------------------------------------------------ */
function setAddAutoComplete ()
{
	setSearchAutoComplete ("metier");
}

/* ------------------------------------------------------------------------------------------------ */
/* setSearchAutoComplete																			*/
/* ------------------------------------------------------------------------------------------------ */
function setSearchAutoComplete (fieldName)
{
	if (fieldName == undefined)
		fieldName = "queryMetier";

	var options = {script			: "autocomplete.php?fieldName=metier&fieldId=metier&table=marpad_lessons&limit=10&",
				   varname			: "input",
				   shownoresults	: false,
			   	   timeout			: 500000,
				   maxresults		: 10,
				   cache			: false
				  };

	var theMetier = new bsn.AutoSuggest(fieldName, options);
}

/* ------------------------------------------------------------------------------------------------ */
/* showHideDownloadBox																				*/
/* ------------------------------------------------------------------------------------------------ */
function showHideDownloadBox ()
{
	if (document.getElementById("downloadLessonBox").style.display == "")
		document.getElementById("downloadLessonBox").style.display = "none";
	else
	{
		document.getElementById("addLessonFeedback").style.display = "none";
		document.getElementById("downloadLessonBox").style.display = "";
	}
}

/* ------------------------------------------------------------------------------------------------ */
/* openForgotPasswordBox																			*/
/* ------------------------------------------------------------------------------------------------ */
function openForgotPasswordBox ()
{
	document.getElementById("forgotPasswordBox").style.display = "";
	document.getElementById("forgotPasswordSep").style.display = "";
}

/* ------------------------------------------------------------------------------------------------ */
/* submitForgotPasswordForm																			*/
/* ------------------------------------------------------------------------------------------------ */
function submitForgotPasswordForm ()
{
	var formValidator = new Validator('forgotPasswordForm');
			
	formValidator.clearAllValidations ();
	formValidator.addValidation('email',			'required',		'יש להזין כתובת מייל');
	formValidator.addValidation('email',			'email',		'כתובת מייל לא חוקית');

	return formValidator.validate ();
}


