var optionZero
function over(tr)
{
	
	tr.style.backgroundColor="#88b8df";
}
function out(tr,type)
{
	
	switch(type)
	{
		case "free":tr.style.backgroundColor="#cad9e5";
					break;
		case "pay1":tr.style.backgroundColor="#47ACFF";
					break;
		case "pay2":tr.style.backgroundColor="#FFEA00";
					break;
	}
	
}
function show_models(select)
{ 
	//alert("YES")
	optionZero=document.getElementById("Related").options[0].text;
	if(select.selectedIndex==0)
	{
		document.getElementById("Related").innerHTML="";
		var newOpt=document.createElement('option');
		newOpt.value="error";
		newOpt.text=optionZero;
		newOpt.style.fontSize="11px";
		newOpt.style.fontFamily="arial";
		newOpt.style.fontWeight="normal";
		try{document.getElementById("Related").add(newOpt,null)}//for W3C
		catch(ex){document.getElementById("Related").add(newOpt)}//for IE
		return;
	}
	else
		document.getElementById("Related").innerHTML="";
	formID=select.options[select.selectedIndex].value;
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
  	{
  		alert ("Your browser does not support AJAX!");
  		return;
  	} 
 
	var url="cms/posts/updateRelated.php?formID="+formID;
	xmlHttp.onreadystatechange=stateChanged;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
	
}

var nextTr
var currpostID
function showPost(postID,mainID,tr,admin)
{
	try
	{	
		var style1=document.getElementById("postTable"+postID).style
		var style2=document.getElementById("postPicture"+postID).style
		
		if(style1.display=="")
		{
			document.getElementById("opcl"+postID).innerHTML="פתח"
			style1.display="none";
			style2.display="none";	
		}
		else
		{
			document.getElementById("opcl"+postID).innerHTML="סגור"
			style1.display="";
			style2.display="";	
		}
		return
	}
	catch(e)
	{
		
		document.getElementById("opcl"+postID).innerHTML="סגור"
		nextTr=tr.nextSibling;
		xmlHttp=GetXmlHttpObject();
		if (xmlHttp==null)
  		{
  			alert ("Your browser does not support AJAX!");
  			return;
  		} 
  		currpostID=postID
  		var url="readPostDetails.php?postID="+postID+"&mainID="+mainID+"&admin="+admin;
  		xmlHttp.onreadystatechange=getPost;
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);
	}
  	
  	
}
function GetXmlHttpObject()
{
	var xmlHttp1=null;
	try
  	{
 	 	// Firefox, Opera 8.0+, Safari
  		xmlHttp1=new XMLHttpRequest();
  	}
	catch (e)
  	{
  		// Internet Explorer
  		try
    	{
    		xmlHttp1=new ActiveXObject("Msxml2.XMLHTTP");
    	}
  		catch (e)
    	{
    		xmlHttp1=new ActiveXObject("Microsoft.XMLHTTP");
    	}
  	}
	return xmlHttp1;
}

function stateChanged() 
{ 
	if (xmlHttp.readyState==4)
	{ 
		//alert(xmlHttp.responseText)
		var result=xmlHttp.responseText.split("|");
		var newOpt=document.createElement('option')
		newOpt.value="error"
		newOpt.text=optionZero;
		try{document.getElementById("Related").add(newOpt,null)}//for W3C
		catch(ex){document.getElementById("Related").add(newOpt)}//for IE
		for(var i=0;i<result.length;i=i+2)
		{
			newOpt=document.createElement('option');
			newOpt.value=result[i];
			newOpt.text=result[i+1]
			try{document.getElementById("Related").add(newOpt,null)}//for W3C
			catch(ex){document.getElementById("Related").add(newOpt)}//for IE
		}
		try{document.getElementById("selected_related").value;
			selectRelated(document.getElementById("selected_related").value);	}
		catch(e){return}
		
	}
}
function getPost()
{
	if (xmlHttp.readyState==4)
	{
		var postExt=xmlHttp.responseText;
		var tbody=document.getElementById("postsTable");
		numTh=tbody.getElementsByTagName("th")//determine the colspan num
		var newTr=document.createElement("tr");
		newTr.style.width="100%";
		var newTd=document.createElement("td");
		newTd.style.width="100%";
		newTd.colSpan=numTh.length;
		newTd.style.backgroundColor="#ecf2f8";
		var postTr=tbody.insertBefore(newTr,nextTr);
		postTd=postTr.appendChild(newTd);
		postTd.innerHTML=postExt;
		

	}
}
function changePicture(currDiv,postID)
{
	pastDiv=document.getElementById("selectedPic"+postID);
	pastDiv.id="";
	
	pastDiv.style.border="0px solid #4b82af";
	currDiv.style.border="1px solid #4b82af"
	currImgsrc=currDiv.lastChild.name;
	
	bigImg=document.getElementById("bigPicture"+postID);
	bigImg.src="posts_images/"+currImgsrc;
	currDiv.id="selectedPic"+postID
}
function checkClientForm(form,pass)
{
	
	//must fields
	if(form.FirstName.value=="" || form.LastName.value=="" || form.Email.value=="" /*|| form.Phone1.value==""*/)
	{
		alert("יש למלא את כל שדות החובה")
		return false;
	}
	else
	{
		//Email validation
		re=/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		if(form.Email.value.match(re)==null)
		{
			alert("דואר אלקטרוני לא חוקי");
			return false;
		}
		

		/*if(!isPosInteger(form.Phone1.value))
		{
			alert("טלפון ראשי חייב להיות מספרי");
			return false;
		}
		if(form.Phone2.value != '' && !isPosInteger(form.Phone2.value))
		{
			alert("טלפון משני חייב להיות מספרי");
			return false;
		}*/
	}
	
	//if its new user that admin wants to enter we must the password
	if(pass==1)
	{
		//Must be at least 6 characters 
		//Must contain at least one one lower case letter, one upper case letter, one digit and one special character 
		//Valid special characters (which are configurable) are -   @#$%^&+=
		
		if(form.Password.value!=form.repPassword.value)
		{
			alert("אימות סיסמא נכשל! הקלד את הסיסמא שוב")
			form.Password.value="";
			form.repPassword.value=""
			return false;
		}
		else
		{
			if(form.Password.value.length<6)
			{
				alert("הסיסמא חייבת להיות באורך של 6 תוים לפחות")
				form.Password.value="";
				form.repPassword.value="";
				return false;
			}
		}
	}
	return true;
}
function isPosInteger(inputStr) 
{
	var re = /^\d*$/;
	inputStr = inputStr.toString();
	if (!inputStr.match(re)) {
		return false;
	}
	return true;
}


function enableEditPost(checkBox)
{
	inputs=document.getElementsByTagName("input")
	selects=document.getElementsByTagName("select")
	if(checkBox.checked==true)
	{
		if(!confirm("האם אתה בטוח רוצה לשנות את תוכן המודעה?"))
		{
			checkBox.checked=false;
			return;
		}
		for(var i=0;i<inputs.length;i++)
		{
			if(inputs[i].id!="enable")
				inputs[i].disabled=false;
		}
		for(i=0;i<selects.length;i++)
		{
			selects[i].disabled=false;
		}
		document.getElementById("Description").disabled=false;
	}
	else
	{
		for(var i=0;i<inputs.length;i++)
		{
			if(inputs[i].id!="enable" && inputs[i].id!="back" )
				inputs[i].disabled=true;
		}
		for(i=0;i<selects.length;i++)
		{
			selects[i].disabled=true;
		}
		document.getElementById("Description").disabled=true;
	}
}
function selectRelated(currValue)
{
	if(currValue=="")
		return;
	else
	{
		select=document.getElementById("Related");
		for(var i=0;i<select.length;i++)
		{
			if(select.options[i].text==currValue)
			{
				select.options[i].selected=true;
				return;
			}
		}
	}
}
function updateAction(select)
{
	sForm=document.getElementById("mainSearch");
	mainID=select.options[select.selectedIndex].value;
	sForm.action="postsTable.php?mainID="+mainID+"&mainSearch=1";
}
function checkContact()
{
	re=/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if(document.getElementById("Email1").value.match(re)==null)
	{
		alert("דואר אלקטרוני לא חוקי");
		return false;
	}
	if(document.getElementById("Name").value=="")
	{
		alert("שם הפונה הינו שדה חובה למילוי!")
		return false;
	}
	return true;
}
function checkNewAgent()
{
	if(document.getElementById("Mustsub").options[document.getElementById("Mustsub").selectedIndex].value=='error')
	{
		alert("חובה להגדיר שלונית עבור הסוכן")
		return false;
	}
	re=/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if(document.getElementById("Email").value.match(re)==null)
	{
		alert("דואר אלקטרוני לא חוקי");
		return false;
	}
	return true;
}
function onlyPicShow(checkBox)
{
	if(checkBox.checked==true)
	{
		/*var newInput=document.createElement("input");
		newInput.type='hidden'
		newInput.name='showPic';
		newInput.id='yes';
		currForm=document.getElementById('searcForm');
		currForm.appendChild(newInput);*/
		SetCookie('showPicOnly','yes',1)
		location.href=location.href
		
	}
	else
	{
		delete_cookie('showPicOnly');
		location.href=location.href
	}
}
function SetCookie(cookieName,cookieValue,nDays)
 {
	 var today = new Date();
 	var expire = new Date();
 	if (nDays==null || nDays==0) nDays=1;
 	expire.setTime(today.getTime() + 3600000*24*nDays);
	 document.cookie = cookieName+"="+escape(cookieValue)
                 + ";expires="+expire.toGMTString();
}
function delete_cookie ( cookie_name ) {	
	var cookie_date = new Date ( );  // current date & time
    cookie_date.setTime ( cookie_date.getTime() - 1 );
    document.cookie = cookie_name += "=; expires=" + cookie_date.toGMTString();
}
function newWindow(postID) {
	window.open('sendEmail.php?postID='+postID,'mywindow','width=500,height=500');
}
function newWindowFriend(postID,mainID){
	window.open('sendFriend.php?postID='+postID+'&mainID='+mainID,'mywindow','width=500,height=500');
	
}
function checksendMail() {
	
	if(document.getElementById("Name").value=='')
	{
		alert("שם השולח הוא שדה חובה");
		return false;
	}
	re=/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if(document.getElementById("Email").value.match(re)==null)
	{
		alert("דואר אלקטרוני לא חוקי");
		return false;
	}
	
	return true;
}

function paymentForm(type)
{
	switch (type)
	{
		case "free":var paymentsTr=document.getElementsByTagName("TR");
					for(var i=paymentsTr.length-1;i>=0;i--)
					{
						
						if(paymentsTr[i].id.indexOf("payment")!=-1)
						{
							document.getElementsByTagName("TBODY")[0].removeChild(paymentsTr[i]);
						}
					}
					break;
		case "pay1":if(document.getElementById("payment_1")==null)
						createPaymentTable();
					break;
		case "pay2":if(document.getElementById("payment_1")==null)
						createPaymentTable();
					break;
	}
}
function createPaymentTable()
{
	
	
		var beforeTr=document.getElementById("beforePaymentForm")
		titleTr=document.createElement("tr");
		titleTr.id="payment_1"
		var titleTh=document.createElement("th");
		titleTh.className="postdetext";
		titleTh.colSpan="2";
		var titleMessage=document.createTextNode("פרטי התשלום");
		titleTh.appendChild(titleMessage);
		titleTr.appendChild(titleTh);
		document.getElementsByTagName("TBODY")[0].insertBefore(titleTr,beforeTr);
					
					
		/////שם בעל כרטיס אשראי 
		var propTr=document.createElement("tr");
		propTr.id="payment_2"
		var propTd=document.createElement("td");
		var titleTd=document.createTextNode("שם בעל כרטיס האשראי");
		var inputName=document.createElement("input");
					
		var starSpan=document.createElement("span");
		starSpan.style.color="red";
		var star=document.createTextNode("*");
		starSpan.appendChild(star)
					
		inputName.type="text";
		inputName.name="ccard_owner_name";
		inputName.id="ccard_owner_name";
		propTd.appendChild(starSpan);
		propTd.appendChild(titleTd)
		propTr.appendChild(propTd);
					
		propTd=document.createElement("td");
		propTd.appendChild(inputName)
		propTr.appendChild(propTd);
		document.getElementsByTagName("TBODY")[0].insertBefore(propTr,beforeTr);
		////////////////////////////////
					
		//////מספר תעודת זהות
		var propTr=document.createElement("tr");
		propTr.id="payment_3"
		var propTd=document.createElement("td");
		var titleTd=document.createTextNode("מספר תעודת זהות של בעל הכרטיס");
		var inputName=document.createElement("input");
		var starSpan=document.createElement("span");
		starSpan.style.color="red";
	    var star=document.createTextNode("*");
		starSpan.appendChild(star);
					
		inputName.type="text";
		inputName.name="ccard_owner_id";
		inputName.id="ccard_owner_id";
		inputName.maxLength="9"
		propTd.appendChild(starSpan);
		propTd.appendChild(titleTd)
		propTr.appendChild(propTd);
					
		propTd=document.createElement("td");
		propTd.appendChild(inputName)
		propTr.appendChild(propTd);
		document.getElementsByTagName("TBODY")[0].insertBefore(propTr,beforeTr);
					
		///סוג הכרטיס
		var propTr=document.createElement("tr");
		propTr.id="payment_4"
		var propTd=document.createElement("td");
		var titleTd=document.createTextNode("סוג הכרטיס");
	    var selectType=document.createElement("select");
		var starSpan=document.createElement("span");
		starSpan.style.color="red";
		var star=document.createTextNode("*");
		starSpan.appendChild(star);
		propTd.appendChild(starSpan);
					
		propTd.appendChild(titleTd)
		propTr.appendChild(propTd);
		selectType.name="ccard_type";
		selectType.id="ccard_type";
		var newOpt=document.createElement("option");
		newOpt.value="-1";
		newOpt.text="בחר כרטיס אשראי";
		try{selectType.add(newOpt,null)}//for W3C
		catch(ex){selectType.add(newOpt)}//for IE
		options=new Array("American Express","Diner's Club","Leumi Card","Isracard","Master Card","Cal Visa");
					for(i=0;i<options.length;i++)
					{
						var newOpt=document.createElement("option");
						newOpt.value=options[i];
						newOpt.text=options[i];
						try{selectType.add(newOpt,null)}//for W3C
						catch(ex){selectType.add(newOpt)}//for IE
					}
					
					propTd=document.createElement("td");
					propTd.appendChild(selectType)
					propTr.appendChild(propTd);
					document.getElementsByTagName("TBODY")[0].insertBefore(propTr,beforeTr);
					
					///מספר הכרטיס 
					var propTr=document.createElement("tr");
					propTr.id="payment_5"
					var propTd=document.createElement("td");
					var titleTd=document.createTextNode("מספר הכרטיס");
					var inputName=document.createElement("input");
					var starSpan=document.createElement("span");
					starSpan.style.color="red";
					var star=document.createTextNode("*");
					starSpan.appendChild(star);
					propTd.appendChild(starSpan);
					inputName.type="text";
					inputName.name="ccard_number";
					inputName.id="ccard_number";
					inputName.maxLength="16"
					propTd.appendChild(titleTd)
					propTr.appendChild(propTd);
					
					propTd=document.createElement("td");
					propTd.appendChild(inputName)
					propTr.appendChild(propTd);
					document.getElementsByTagName("TBODY")[0].insertBefore(propTr,beforeTr);
					
					///תוקף הכרטיס
					var propTr=document.createElement("tr");
					propTr.id="payment_6"
					var propTd=document.createElement("td");
					var titleTd=document.createTextNode("תוקף הכרטיס");
					var selectType=document.createElement("select");
					var starSpan=document.createElement("span");
					starSpan.style.color="red";
					var star=document.createTextNode("*");
					starSpan.appendChild(star);
					propTd.appendChild(starSpan);
					
					propTd.appendChild(titleTd)
					propTr.appendChild(propTd);
					selectType.name="ccard_exp_date_Month";
					selectType.id="ccard_exp_date_Month";
					var newOpt=document.createElement("option");
					newOpt.value="-1";
					newOpt.text="בחר חודש";
					try{selectType.add(newOpt,null)}//for W3C
					catch(ex){selectType.add(newOpt)}//for IE
					for(i=1;i<13;i++)
					{
						var newOpt=document.createElement("option");
						newOpt.value=i;
						newOpt.text=i;
						try{selectType.add(newOpt,null)}//for W3C
						catch(ex){selectType.add(newOpt)}//for IE
					}
					
					propTd=document.createElement("td");
					propTd.appendChild(selectType)
					
					var selectType=document.createElement("select");
					selectType.name="ccard_exp_date_Year";
					selectType.id="ccard_exp_date_Year";
					var newOpt=document.createElement("option");
					newOpt.value="-1";
					newOpt.text="בחר שנה";
					try{selectType.add(newOpt,null)}//for W3C
					catch(ex){selectType.add(newOpt)}//for IE
					for(i=2008;i<2021;i++)
					{
						var newOpt=document.createElement("option");
						newOpt.value=i;
						newOpt.text=i;
						try{selectType.add(newOpt,null)}//for W3C
						catch(ex){selectType.add(newOpt)}//for IE
					}
					propTd.appendChild(selectType)
					propTr.appendChild(propTd);
					document.getElementsByTagName("TBODY")[0].insertBefore(propTr,beforeTr);
					////////////////////////
					///מספר בגב הכרטיס
					var propTr=document.createElement("tr");
					propTr.id="payment_7"
					var propTd=document.createElement("td");
					var titleTd=document.createTextNode("3 ספרות אחרונות בגב הכרטיס");
					var inputName=document.createElement("input");
					var starSpan=document.createElement("span");
					starSpan.style.color="red";
					var star=document.createTextNode("*");
					starSpan.appendChild(star);
					propTd.appendChild(starSpan);
					
					inputName.type="text";
					inputName.name="ccard_cvv";
					inputName.id="ccard_cvv";
					inputName.maxLength="3"
					inputName.size="3"
					propTd.appendChild(titleTd)
					propTr.appendChild(propTd);
					
					propTd=document.createElement("td");
					propTd.appendChild(inputName)
					propTr.appendChild(propTd);
					document.getElementsByTagName("TBODY")[0].insertBefore(propTr,beforeTr);
					
					///דואר אלקטרוני
					
					
					var propTr=document.createElement("tr");
					propTr.id="payment_8"
					var propTd=document.createElement("td");
					var titleTd=document.createTextNode("דואר אלקטרוני של בעל הכרטיס");
					var inputName=document.createElement("input");
					var starSpan=document.createElement("span");
					starSpan.style.color="red";
					var star=document.createTextNode("*");
					starSpan.appendChild(star);
					propTd.appendChild(starSpan);
					
					inputName.type="text";
					inputName.name="Email";
					inputName.id="Email";
					
					propTd.appendChild(titleTd)
					propTr.appendChild(propTd);
					
					propTd=document.createElement("td");
					propTd.appendChild(inputName)
					propTr.appendChild(propTd);
					document.getElementsByTagName("TBODY")[0].insertBefore(propTr,beforeTr);
	
}
function trim(str) 
{  
	if(str.charAt(0) == " ")
  	{  
  		str = trim(str.substring(1));
  	}
 	if (str.charAt(str.length-1) == " ")
  	{  
  		str = trim(str.substring(0,str.length-1));
  	}
  return str;
}

   

