	/*
		Programmer		   :	Wasim Sakharkar
		File Name		   :	Wsim_common_scripts.js
		Functions Names	   :	function Trim(strValue)												Default for all
								func_ValidateForm_search()											search.tpl
								function func_CallingForm(strPath, strFormName, strCallingForm)		search.tpl
								func_Type_search(value)												search.tpl
								func_ValidateGroupCheck(strFormName, strIDField, strCountField)		search_content.tpl, manage_search_keyword.tpl
								func_MultipleSelectDeselect(strFormName, strComponentName)			manage_search_keyword.tpl
								func_CheckUncheck(strFormName, strComponentName, status)			search_content.tpl
								func_SaveSearchKeyword()											search_content.tpl
								func_ValidateForm_SaveSearchKeyword()								save_search_keyword.php
								func_ValidateForm_SearchSendMail(strFormName)						search_send_mail.php, mail_cart_items.tpl
								func_RemoveMailCartItems(strPath, strFormName, strIdsName, strCountIds, strRemoveId, strCallingForm)       mail_cart_items.tpl
								function func_VisitorSubscribeUnsubscribe(strPath, strPurpose)		visitor_sub_unsubscribe.tpl
	*/

	/* Remove extra spaces from the String */
	function Trim(strValue)
	{  
		while(strValue.charAt(0) == (" ") )
		{
			strValue = strValue.substring(1);
		}
		while(strValue.charAt(strValue.length-1) == " " )
		{
			strValue = strValue.substring(0,strValue.length-1);
		}
		return strValue;
	}


	/* Validate function for search.tpl */
	function func_ValidateForm_search()
	{
		var strForm		= document.frmSearch;
		var strKeyword  = Trim( strForm.keyword.value );

		if (strKeyword.length == 0)
		{
			alert("Please enter your search keyword.");
			strForm.keyword.select();	
			return(false);
		}
	}	// function func_ValidateForm_search() /* Validate function for search.tpl */


	/* Validate function for search.tpl */
	function func_Type_search(value)
	{
		if (value != null)
		{
			var Me = document.frmSearch;
			switch (value)
			{
				case "3":	// Product
					Me.browse.value="product";break;
				case "4":	// Companies
					Me.browse.value="profile";break;
				case "2":	// Buy Offers
					Me.browse.value="buy";break;
				case "1":	// Sell Offers
					Me.browse.value="sell";break;
				default:
					Me.browse.value="product";break;
			}			
		}
	}	// function func_Type_search(value) /* Validate function for search.tpl */

	
	/* Calling Form function search.tpl */
	function func_CallingForm(strPath, strFormName, strCallingForm)
	{	
		var strForm = eval('document.'+strFormName);
		strForm.method="post";
		strForm.action=strPath + '/' + strCallingForm;
		strForm.submit();
	}	// function func_CallingForm(strPath, strFormName, strCallingForm)

	
	/* Validate function for search_content.tpl*/
	function func_SearchContent(intType)
	{
		alert("Delete this function");	
		document.frmSaveSearchKeyword.method="post";
		document.frmSaveSearchKeyword.action="search.php?type="+intType;
		document.frmSaveSearchKeyword.submit();
	}	// function func_SearchContent(intType)
	

	/*	Validate function for: 
			search_content.tpl, 
			manage_search_keyword.tpl */
	function func_ValidateGroupCheck(strFormName, strIDField, strCountField)
	{
		var Me = eval('document.'+strFormName);

		eval('document.'+strFormName+'.'+strIDField+'.value = 0');
		eval('document.'+strFormName+'.'+strCountField+'.value = 0');

		// find out all elements present in form having name start with chkMail.
		for(i=0;i<=Me.length-1;i++)
		{
			var chkName = new String(Me.elements[i].name);
			
			if(chkName.substr(0,11)=="chkCheckBox")
			{
				if(Me.elements[i].checked==true) 
				{
					Me.CartId.value = Me.CartId.value +","+ Me.elements[i].value;	// assign selected vlaues sepraed by comma.
					Me.CartCount.value = eval(Me.CartCount.value)+1;	// for selected count purpose.
				}
			}	// if(chkName.substr(0,7)=="chkMail")
		}	// for(i=0;i<=Me.length-1;i++)

		Me.CartId.value = Me.CartId.value + ",0";	// assign last element as default vale as ,0.

		return(true);
	}	// function func_ValidateEMailSend()



	/* Validate function for manage_search_keyword.tpl */
	function func_MultipleSelectDeselect(strFormName, strComponentName)
	{
		if (eval('document.'+strFormName+'.'+strComponentName+'.checked') == true) func_CheckUncheck(strFormName, strComponentName, 'Check all');
		if (eval('document.'+strFormName+'.'+strComponentName+'.checked') == false) func_CheckUncheck(strFormName, strComponentName, 'Uncheck all');
	}	// function func_MultipleSelectDeselect(strFormName, strComponentName)


	/* Validate function for search_content.tpl */
	function func_CheckUncheck(strFormName, strComponentName, status)
	{
		var Me = eval('document.'+strFormName);

		if (status != null)
		{
			// find out all elements present in form having name start with chkMail.
			for(i=0;i<=Me.length-1;i++)
			{
				var chkName = new String(Me.elements[i].name);
				
				if(chkName.substr(0,11)=="chkCheckBox")
				{
					if(status == "Check all")	
					{						
						if(Me.elements[i].checked==false) Me.elements[i].checked=true;
					}
					else if (status == "Uncheck all")	
					{
						if(Me.elements[i].checked==true) Me.elements[i].checked=false;
					}
				}	// if(chkName.substr(0,7)=="chkMail")
			}	// for(i=0;i<=Me.length-1;i++)
			
			if ((strFormName == 'frmSearchContent')|| (strFormName == 'massInquiry'))
			{
				if(status == "Check all") Me.check_all.value ="Uncheck all";	
				if (status == "Uncheck all") Me.check_all.value ="Check all";
			}	// if (strFormName == "frmSearchContent")
		}	// if (status != null)	
	}	// function func_CheckUncheck(status)



	/* Validate function for search_content.tpl */
	function func_SaveSearchKeyword()
	{
		var Me = document.frmSaveSearchKeyword;

		Me.action="save_search_keyword.php";
		window.open("","win","top=5,left=30,toolbars=no,maximize=no,resize=yes,width=616,height=200,location=no,directories=no,scrollbars=no");
		Me.method="post";
		Me.target="win";
		Me.submit();
	}	// function func_ValidateSaveSearchKeyword()


	/* Validate function for save_search_keyword.php */
	function func_ValidateForm_SaveSearchKeyword()
	{
		var strForm		   = document.frmSaveSearchKeyword;
		var strSearchTitle = Trim( strForm.search_title.value );

		if (strSearchTitle.length == 0)
		{
			alert("Please specify search title.");
			strForm.search_title.select();	
			return(false);
		}
	}	// function func_ValidateForm_SaveSearchKeyword() 


	/* Validate function for search_send_mail.php */
	function func_ValidateForm_SearchSendMail(strFormName)
	{
		var strMsg		= "";
		var strForm		= eval('document.'+strFormName);
		var strSubject	= Trim( strForm.subject.value ); 
		var strMessage	= Trim( strForm.message.value ); 
			
		if (strSubject.length == 0) strMsg= strMsg +"Please specify Subject. \n";
		if (strMessage.length == 0)	strMsg= strMsg +"Please specify Message. \n";
		
		if (strSubject.length != 0)
		{
			if(strSubject.match(/[&<>]+/)) strMsg= strMsg +"Please remove Invalid characters from Subject (e.g. &  < >) \n";
		}	// if (strSubject.length != 0)

		if (strMessage.length != 0)
		{
			if(strMessage.match(/[&<>]+/)) strMsg= strMsg +"Please remove Invalid characters from Message (e.g. &  < >) \n";
		}

		if (strMsg.length != 0)
		{
			alert(strMsg);	
			return false;
		}	// if (strMsg.length != 0)
	}	// function func_ValidateForm_SaveSearchKeyword() 


	/* Remove items from mail cart  mail_cart_items.tpl */
	function func_RemoveMailCartItems(strPath, strFormName, strIdsName, strCountIds, strRemoveId, strCallingForm)
	{
		var Me = eval('document.'+strFormName);
		var strIds = Trim( eval('document.'+strFormName+'.'+strIdsName+'.value') );
		var strOldValue = ","+strRemoveId+",";
		var strNewValue = ",0,";

		if (strIds != null )
		{
			strIds = strIds.replace( strOldValue, strNewValue );
			eval('document.'+strFormName+'.'+strIdsName+'.value = strIds');
			eval('document.'+strFormName+'.'+strCountIds+'.value = document.'+strFormName+'.'+strCountIds+'.value-1');
			eval('document.'+strFormName+'.itemRemove.value = 1');

			func_CallingForm(strPath, strFormName, strCallingForm)

		}
	}	// function func_RemoveMailCartItems()



	/* Calling Form function visitor_sub_unsubscribe.tpl */
	function func_VisitorSubscribeUnsubscribe(strPath, strPurpose)
	{		
		var strCallingForm = "";

		if( strPurpose != null )
		{
			if( strPurpose=="Subscribe" ) strCallingForm = "visitor_subscription.php?purpose=Subscribe";
			if( strPurpose=="Unsubscribe" ) strCallingForm = "visitor_subscription.php?purpose=Unsubscribe";

			location.href=strPath + '/' + strCallingForm;

		}	// if( strPurpose != null )

	}	// function func_VisitorSubscribeUnsubscribe()

	
	/* Calling Form function visitor_sub_unsubscribe.tpl */
	function func_ValidateForm_subscription(strPurpose, strFormName)
	{
		var strMsg		= "";
		var strForm		= eval('document.'+strFormName);

		if( strPurpose != null )
		{
			if( strPurpose=="Subscribe" )
			{
				var strSubscribeEmail			= Trim( strForm.subscribe_email.value ); 
				var strSubscribeConfirmEmail	= Trim( strForm.subscribe_confirm_email.value ); 

				if (strSubscribeEmail.length == 0) strMsg= strMsg +"Please specify Email. \n";
				if (strSubscribeConfirmEmail.length == 0) strMsg= strMsg +"Please specify Confirm Email. \n";

				if (strSubscribeEmail.length != 0)
				{
					if(strSubscribeEmail.match(/[&<>]+/)) strMsg= strMsg +"Please remove Invalid characters from Email (e.g. &  < >) \n";

				}	// if (strSubject.length != 0)

				if (strSubscribeEmail.length != 0)
				{
					var intAtSign, intDot, intComma, intSpace, intLastDot, intDomain, intStrLen;
					
					intAtSign=strSubscribeEmail.indexOf("@");
					intDot1=strSubscribeEmail.indexOf(".");
					intDot=strSubscribeEmail.indexOf(".",intAtSign);
					intComma=strSubscribeEmail.indexOf(",");
					intAtRate1=strSubscribeEmail.indexOf("@");
					intAtRate2=strSubscribeEmail.lastIndexOf("@");
					intSpace=strSubscribeEmail.indexOf(" ");
					intLastDot=strSubscribeEmail.lastIndexOf(".");
					intfirstDot=strSubscribeEmail.indexOf(".");
					intDomain=intDot-intAtSign;
					intStrLen=strSubscribeEmail.length;
					
					if (intAtSign == -1)
						strMsg = strMsg + "Invalid E-Mail Address! @ sign is missing. \n";

					if (intAtSign == 0 )
						strMsg = strMsg + "Invalid E-Mail Address! Email ID cannot start with @ sign. \n";

					if (intAtRate1 != intAtRate2)
						strMsg = strMsg + "Invalid E-Mail Address! Two @ sign are not allowed. \n";

					if (intDot1 == -1)
						strMsg = strMsg + "Invalid E-Mail Address! Dot sign is missing. \n";

					if ((intStrLen-(intLastDot+1)) < 2)
						strMsg = strMsg + "Invalid E-Mail Address! At least two characters required after dot. \n";

					if (intSpace != -1)
						strMsg = strMsg + "Invalid E-Mail Address! Spaces are not allowed. \n";

					if (intComma != -1)
						strMsg = strMsg + "Invalid E-Mail Address! Comma is not allowed. \n";

					if ((intDot <= 2) || (intDomain <= 1)) 
						strMsg = strMsg + "Invalid E-Mail Address! Atleast one valid character required between @ sign and Dot. \n";
				}		

				if (strSubscribeConfirmEmail.length != 0)
				{
					if(strSubscribeConfirmEmail.match(/[&<>]+/)) strMsg= strMsg +"Please remove Invalid characters from Confirm Email (e.g. &  < >) \n";

				}	// if (strSubject.length != 0)

				if (strSubscribeConfirmEmail.length != 0)
				{
					var intAtSign, intDot, intComma, intSpace, intLastDot, intDomain, intStrLen;
					
					intAtSign=strSubscribeConfirmEmail.indexOf("@");
					intDot1=strSubscribeConfirmEmail.indexOf(".");
					intDot=strSubscribeConfirmEmail.indexOf(".",intAtSign);
					intComma=strSubscribeConfirmEmail.indexOf(",");
					intAtRate1=strSubscribeConfirmEmail.indexOf("@");
					intAtRate2=strSubscribeConfirmEmail.lastIndexOf("@");
					intSpace=strSubscribeConfirmEmail.indexOf(" ");
					intLastDot=strSubscribeConfirmEmail.lastIndexOf(".");
					intfirstDot=strSubscribeConfirmEmail.indexOf(".");
					intDomain=intDot-intAtSign;
					intStrLen=strSubscribeConfirmEmail.length;
					
					if (intAtSign == -1)
						strMsg = strMsg + "Invalid Confirm E-Mail Address! @ sign is missing. \n";

					if (intAtSign == 0 )
						strMsg = strMsg + "Invalid Confirm E-Mail Address! Email ID cannot start with @ sign. \n";

					if (intAtRate1 != intAtRate2)
						strMsg = strMsg + "Invalid Confirm E-Mail Address! Two @ sign are not allowed. \n";

					if (intDot1 == -1)
						strMsg = strMsg + "Invalid Confirm E-Mail Address! Dot sign is missing. \n";

					if ((intStrLen-(intLastDot+1)) < 2)
						strMsg = strMsg + "Invalid Confirm E-Mail Address! At least two characters required after dot. \n";

					if (intSpace != -1)
						strMsg = strMsg + "Invalid Confirm E-Mail Address! Spaces are not allowed. \n";

					if (intComma != -1)
						strMsg = strMsg + "Invalid Confirm E-Mail Address! Comma is not allowed. \n";

					if ((intDot <= 2) || (intDomain <= 1)) 
						strMsg = strMsg + "Invalid Confirm E-Mail Address! Atleast one valid character required between @ sign and Dot. \n";
				}		

				if( strSubscribeEmail.length != 0 && strSubscribeConfirmEmail.length != 0 )
				{
					if( strSubscribeEmail !=  strSubscribeConfirmEmail ) strMsg = strMsg + "Email address you entered do not match! \n";
				}

			}	// if( strPurpose=="Subscribe" )

			if( strPurpose=="Unsubscribe" )
			{
				var strUnsubscribeEmail			= Trim( strForm.unsubscribe_email.value ); 

				if (strUnsubscribeEmail.length == 0) strMsg= strMsg +"Please specify Email. \n";

				if (strUnsubscribeEmail.length != 0)
				{
					if(strUnsubscribeEmail.match(/[&<>]+/)) strMsg= strMsg +"Please remove Invalid characters from Email (e.g. &  < >) \n";

				}	// if (strUnsubscribeEmail.length != 0)

				if (strUnsubscribeEmail.length != 0)
				{
					var intAtSign, intDot, intComma, intSpace, intLastDot, intDomain, intStrLen;
					
					intAtSign=strUnsubscribeEmail.indexOf("@");
					intDot1=strUnsubscribeEmail.indexOf(".");
					intDot=strUnsubscribeEmail.indexOf(".",intAtSign);
					intComma=strUnsubscribeEmail.indexOf(",");
					intAtRate1=strUnsubscribeEmail.indexOf("@");
					intAtRate2=strUnsubscribeEmail.lastIndexOf("@");
					intSpace=strUnsubscribeEmail.indexOf(" ");
					intLastDot=strUnsubscribeEmail.lastIndexOf(".");
					intfirstDot=strUnsubscribeEmail.indexOf(".");
					intDomain=intDot-intAtSign;
					intStrLen=strUnsubscribeEmail.length;
					
					if (intAtSign == -1)
						strMsg = strMsg + "Invalid E-Mail Address! @ sign is missing. \n";

					if (intAtSign == 0 )
						strMsg = strMsg + "Invalid E-Mail Address! Email ID cannot start with @ sign. \n";

					if (intAtRate1 != intAtRate2)
						strMsg = strMsg + "Invalid E-Mail Address! Two @ sign are not allowed. \n";

					if (intDot1 == -1)
						strMsg = strMsg + "Invalid E-Mail Address! Dot sign is missing. \n";

					if ((intStrLen-(intLastDot+1)) < 2)
						strMsg = strMsg + "Invalid E-Mail Address! At least two characters required after dot. \n";

					if (intSpace != -1)
						strMsg = strMsg + "Invalid E-Mail Address! Spaces are not allowed. \n";

					if (intComma != -1)
						strMsg = strMsg + "Invalid E-Mail Address! Comma is not allowed. \n";

					if ((intDot <= 2) || (intDomain <= 1)) 
						strMsg = strMsg + "Invalid E-Mail Address! Atleast one valid character required between @ sign and Dot. \n";
				}		




			}	// if( strPurpose=="Subscribe" )

			if (strMsg.length != 0)
			{
				alert(strMsg);	
				return false;

			}	// if (strMsg.length != 0)

		}	// if( strPurpose != null )

	}	// function func_ValidateForm_subscription()
