/* -----------------------------------------------
#‎  FreakBox.co.ilכל הזכויות שמורות לליאור וול מ  #
#‎  liorwohl@gmail.com לפרטים נוספים:‏             #
----------------------------------------------- */
//a סקריפט לשליחת טפסים וקבלת ערך בחזרה ללא מעבר עמוד

function ajaxForm(form2use,action)
{
	var args;
	var msg;
	
	this.send = function()
	{
		if (window.XMLHttpRequest)
		{
			//a כל דפדפן נורמלי
			xmlHttp = new XMLHttpRequest();
		}
		else if (window.ActiveXObject)
		{
			//a אקספלורר 6
			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
		}
		else
		{
			//a דפדפנים שלא תומכים באג'קס בכלל
			alert("AJAX not supported. Please upgrade your browser!");
		}
	
		args = "useAjax=true";
		if (form2use)
		{			
			formItems = form2use.getElementsByTagName("*");
			for (i=0; i<formItems.length; i++)
			{
				oitem = formItems[i];
				if (oitem.name)
				{
					if (oitem.tagName.toLowerCase() == "input")
					{
						if (oitem.type.toLowerCase() == "text" || oitem.type.toLowerCase() == "password" || oitem.type.toLowerCase() == "hidden")
						{
							args = ""+args+"&"+oitem.name+"="+oitem.value+"";
						}
						if (oitem.type.toLowerCase() == "checkbox")
						{
							if (oitem.checked)
							{
								args = ""+args+"&"+oitem.name+"=on";
							} 
							else 
							{
								args = ""+args+"&"+oitem.name+"=off";
							}
						}
						if (oitem.type.toLowerCase() == "radio") 
						{
							if (oitem.checked) 
							{
								args = ""+args+"&"+oitem.name+"="+oitem.value+"";
							}
						}
					}	
					if (oitem.tagName.toLowerCase() == "select") 
					{
						args = ""+args+"&"+oitem.name+"="+oitem.getElementsByTagName("option")[oitem.selectedIndex].value+"";
					}
					if (oitem.tagName.toLowerCase() == "textarea") 
					{
						args = ""+args+"&"+oitem.name+"="+oitem.value+"";
					}
				}
			}
		}
		//alert(args); //for debuging
	
		xmlHttp.open("post",action,false);
		xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		xmlHttp.send(args);
		
		xmlDoc = xmlHttp.responseXML.documentElement;
		success = xmlDoc.getElementsByTagName("success")[0].childNodes[0].nodeValue;
		msg = xmlDoc.getElementsByTagName("msg")[0].childNodes[0].nodeValue;
		
		if (success == "false")
		{
			return false;
		}
		return true;
	}
	
	this.getMsg = function()
	{
		return msg;
	}
}

/* ----------------------------------------------
#‎ פונקציות שמכסות את כל סוגי השליחות שצריך      #
#‎ כדי שלא יהיה צריך לכתוב בכל טופס/קישור מחדש.. #
---------------------------------------------- */

function displayFormError(form,error)
{
	if (document.getElementById("errorPlace"))
	{
		document.getElementById("errorPlace").parentNode.removeChild(document.getElementById("errorPlace"));
	}
	
	e = document.createElement("div");
	e.id = "errorPlace";
	e.innerHTML = error;
	form.appendChild(e);
}

//a מציג באלרט את התוצאות של שליחת הטופס, ללא מעבר עמוד.
function alertAjaxFormResults(theForm,theAction)
{
	t = new ajaxForm(theForm,theAction);
	t.send();
	if (!theForm)
		alert(t.getMsg());
	else
		displayFormError(theForm,t.getMsg());
}

//a מציג באלרט את התוצאות של שליחת הטופס, עם מעבר עמוד.
function ajaxFormAlertAndRedirect(theForm,theAction,url)
{
	t = new ajaxForm(theForm,theAction);
	if (t.send())
	{
		alert(t.getMsg());
		window.location = url;
	}
	else
	{
		if (!theForm)
			alert(t.getMsg());
		else
			displayFormError(theForm,t.getMsg());
	}
}

//a מציג באלרט את התוצאות ואז מרפרש את הדף
function ajaxFormAlertAndReload(theForm,theAction)
{
	ajaxFormAlertAndRedirect(theForm,theAction,window.location);
}

//a מעביר לכתובת חדשה אם לא הייתה שגיאה בשליחת הטופס או מציג את השגיאה באלרט ולא מעביר עמוד אם כן הייתה שגיאה
function ajaxFormRedirectOrAlert(theForm,theAction,url)
{
	t = new ajaxForm(theForm,theAction);
	if (t.send())
	{
		window.location = url;
	}
	else
	{
		if (!theForm)
			alert(t.getMsg());
		else
			displayFormError(theForm,t.getMsg());
	}
}

//a מרפרש אם לא הייתה שגיאה בשליחת הטופס או מציג את השגיאה באלרט אם כן הייתה שגיאה
function ajaxFormReloadOrAlert(theForm,theAction)
{
	ajaxFormRedirectOrAlert(theForm,theAction,window.location);
}

//a בלחיצה על הקישור יופיע אלרט אם תהיה שגיאה או מעבר עמוד אם לא תהיה שגיאה
function ajaxLinkRedirectOrAlert(theAction,url)
{
	ajaxFormRedirectOrAlert(null,theAction,url);
}

function ajaxLinkAlertAndReload(theAction)
{
	ajaxFormAlertAndRedirect(null,theAction,window.location);
}

//a בלחיצה על הקישור יופיע אלרט אם תהיה שגיאה או רענון לעמוד אם לא תהיה שגיאה
function ajaxLinkReloadOrAlert(theAction)
{
	ajaxLinkRedirectOrAlert(theAction,window.location);
}

