//trims out he first and the last white space.
function trim_string(str)
{  while(str.charAt(0) == (" ") )
  {  str = str.substring(1);
  }
  while(str.charAt(str.length-1) == " " )
  {  str = str.substring(0,str.length-1);
  }
  return str;
}




/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/		

//trims out he the end white space.
function rtrim_string(str)
{ 
  while(str.charAt(str.length-1) == " " )
  {  str = str.substring(0,str.length-1);
  }
  return str;
}	
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/		
	function carriage_space(input) {
// Converts carriage returns 
// to <BR> for display in HTML

var output = "";
for (var i = 0; i < input.length; i++) {
if ((input.charCodeAt(i) == 13) || (input.charCodeAt(i) == 10)) {
i++;
output += " ";
} else {
output += input.charAt(i);
   }
}
return output;
}
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/	
function check_wordsize(strin, num)
{
	var valid = true;
	
	
	
	if (strin != "")
	{
		//get rid of carriage returns
		var str= carriage_space(strin);
		
		
		var my_ar = str.split(" ");
		//no word can have more than 80 characters.
		for (i=0; i < my_ar.length; i++)
		{
			if (my_ar[i].length >num)
			{
				valid = false;
				break;
			}
		}
	}
		if (!valid)
		{
		
			return false;
		}
		else { return true; }
	}//end of check_wordsize function.


/*~~~~~~~~~~~~~~~~~check date~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/	
var vm = new Array(31,28,31,30,31,30,31,31,30,31,30,31);

function checkdate(month_in, day_in, year_in){
	
	
	if (year_in/4 == parseInt(year_in/4)){
	
	if((year_in/100 == parseInt(year_in/100)) && (year_in/400 != parseInt(year_in/400)))
	{vm[1]=28;}
		else {vm[1]=29;}
		
	}
	
	
	if (day_in <= vm[month_in-1])
	{return true;}
	else {return false;}

	
}



/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/	


function check_fields(str){
		// allow ONLY alphanumeric keys and underscore, no symbols or punctuation
		// this can be altered for any "checkOK" string you desire
	var checkOK = "-_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ 0123456789:'&\@%$"";
	var checkStr = str;
	var valid = true;

				for (i = 0;  i < checkStr.length;  i++)
					{
					ch = checkStr.charAt(i);
					for (j = 0;  j < checkOK.length;  j++)
					if (ch == checkOK.charAt(j))
						break;
		
					if (j == checkOK.length)
						{
						valid = false;
						break;
						}
				}
			
			
			
		if (!valid)
		{
		
			return false;
		}
		else { return true; }
	}

/*~~~~~~~~~~~~~~~~~~address and city~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/	

	


	//URL simple check
	function check_url(str){
		
	var checkNO = "[]{}<>()|,;'\\ \"#^*`";
	var checkStr = str;
	var valid = true;

		for (i = 0;  i < checkStr.length;  i++)
			{
				ch = checkStr.charAt(i);
				for (j = 0;  j < checkNO.length;  j++)
				if (ch == checkNO.charAt(j))
				    {
                    valid = false;
				    break;
                    }
		
				if (j < checkNO.length)
						break;
		      }

			
			
		if (!valid)
			{
			return false;
			}
			else { return true; }
	}//end of function.*/




/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/	
	
	function check_se_form(my_form) {
	
	var problem = false; // Flag variable.
	var txt="";
	
		//check the title name field.
		if (my_form.ti.value == "") 
			{
				txt +="<input type=button value=^ onClick=document.getElementById(\"ti\").focus();> *the Title field is required. <br>";
				problem = true;	
			}
			//remove leading and trailing whitespace from the string 
			else {my_form.ti.value=trim_string(my_form.ti.value);}
		
			
			
			
		// check the category.
		if ((my_form.cg.selectedIndex == 0) || (my_form.cg.value == "") ) 
			{
				txt += "<input type=button value=^ onClick=document.getElementById(\"cg\").focus();> *Please select a category. <br>";
				problem = true;	
			}
		
 			
		//check the description field.
		if (my_form.ds.value == "") 
			{
				txt +="<input type=button value=^ onClick=document.getElementById(\"ds\").focus();> *the Description field is required. <br>";
				problem = true;
			}
		
			
				// check input date and time.
		if ((my_form.s_m.selectedIndex == 0) || (my_form.s_m.value == "")|| (my_form.s_d.selectedIndex == 0) || (my_form.s_d.value == "")|| (my_form.s_y.selectedIndex == 0) || (my_form.s_y.value == "") ) 
			{
				txt += "<input type=button value=^ onClick=document.getElementById(\"s_m\").focus();> *Valid date is required. <br>";
				problem = true;	
			}			
			//check valid date
			else if (!checkdate(my_form.s_m.value, my_form.s_d.value, my_form.s_y.value)){
				txt += "<input type=button value=^ onClick=document.getElementById(\"s_m\").focus();> *Invalid date input. <br>";
				problem = true;	
			}		

						// check input end date .
		if ((my_form.e_m.selectedIndex == 0) || (my_form.e_m.value == "")|| (my_form.e_d.selectedIndex == 0) || (my_form.e_d.value == "")|| (my_form.e_y.selectedIndex == 0) || (my_form.e_y.value == "") ) 
			{/* do nothing */
			}			
			//check valid date
			else if (!checkdate(my_form.e_m.value, my_form.e_d.value, my_form.e_y.value)){
				txt += "<input type=button value=^ onClick=document.getElementById(\"e_m\").focus();> *Invalid date input. <br>";
				problem = true;	
			}	
			
			//check address:street
			if (my_form.st.value == "") 
			{
				txt +="<input type=button value=^ onClick=document.getElementById(\"st\").focus();> *the detailed address is required, please fill in the street field. <br>";
				problem = true;	
			}
			//remove leading and trailing whitespace from the string 
			else {my_form.st.value=trim_string(my_form.st.value);}		
			
			//check address:city
			if (my_form.ci.value == "") 
			{
				txt +="<input type=button value=^ onClick=document.getElementById(\"ci\").focus();> *the detailed address is required, please fill in the city field. <br>";
				problem = true;	
			}
			//remove leading and trailing whitespace from the string 
			else {my_form.ci.value=trim_string(my_form.ci.value);}	
			
			//check address:province
			if (my_form.pr.value == "") 
			{
				txt +="<input type=button value=^ onClick=document.getElementById(\"pr\").focus();> *the detailed address is required, please fill in the province field. <br>";
				problem = true;	
			}
			//remove leading and trailing whitespace from the string 
			else {my_form.pr.value=trim_string(my_form.pr.value);}	
						
			//check address:country
			if (my_form.co.value == "") 
			{
				txt +="<input type=button value=^ onClick=document.getElementById(\"co\").focus();> *the detailed address is required, please fill in the country field. <br>";
				problem = true;	
			}
			//remove leading and trailing whitespace from the string 
			else {my_form.co.value=trim_string(my_form.co.value);}	
						
				
	 		//validate title
 		if (my_form.ti.value == "") {/*do nothing*/}
 			else if (trim_string(my_form.ti.value).length<6) 
 			{
				txt +="<input type=button value=^ onClick=document.getElementById(\"ti\").focus();> *Title: must not less than 6 alphanumeric characters. <br>";
				problem = true;	
			}
			else if (!check_fields(my_form.ti.value=trim_string(my_form.ti.value))) 
 			{
				txt +="<input type=button value=^ onClick=document.getElementById(\"ti\").focus();> *Title: alphanumeric characters only. <br>";
				problem = true;	
			}		

				//check the description length.	
 		var ds_length=my_form.ds.value.length; 
		var ds2_length=(rtrim_string(my_form.ds.value)).length;
 		
		if (my_form.ds.value == "") {/*do nothing*/}
		else if(ds_length > 5000) 
 		{
   			txt +="<input type=button value=^ onClick=document.getElementById(\"ds\").focus();> *Too much data in the description field! Please remove "+ (ds_length - 5000)+ " characters.<br>";
   			problem= true; 
 		}else if(ds2_length < 20) 
 		{
   			txt +="<input type=button value=^ onClick=document.getElementById(\"ds\").focus();> *No enough characters in the description field! Please add "+ (20 - ds2_length)+ " characters.<br>";
   			problem= true; 
 		} else if (!check_wordsize(my_form.ds.value,90)) 
 		{
   			txt +="<input type=button value=^ onClick=document.getElementById(\"ds\").focus();> *At least one single world in Description has more than 90 characters.<br>";
   			problem= true; 
 		}	
			

	 		//validate address
 		if (my_form.st.value == "") {/*do nothing*/}
 			else if ((my_form.st.value=trim_string(my_form.st.value)).search(/^[a-zA-Z0-9]+[a-zA-Z0-9. ,\-\']*[a-zA-Z0-9.]+$/i) == -1) 
 			{
				txt +="<input type=button value=^ onClick=document.getElementById(\"st\").focus();> *Invalid street name. <br>";
				problem = true;	
			}
 		
 		
 			 //validate city
 		if (my_form.ci.value == "") {/*do nothing*/}
 			else if ((my_form.ci.value=trim_string(my_form.ci.value)).search(/^[a-zA-Z]+[a-zA-Z ]*[a-zA-Z]+$/i) == -1) 
 			{
				txt +="<input type=button value=^ onClick=document.getElementById(\"ci\").focus();> *Invalid city name. <br>";
				problem = true;	
			}
 		
 		
 	 		//validate province
 		if (my_form.pr.value == "") {/*do nothing*/}
 			else if ((my_form.pr.value=trim_string(my_form.pr.value)).search(/^[a-zA-Z]+[a-zA-Z ]*[a-zA-Z]+$/i) == -1) 
 			{
				txt +="<input type=button value=^ onClick=document.getElementById(\"pr\").focus();> *Invalid province name. <br>";
				problem = true;	
			}	
 		
  			//validate city
 		if (my_form.co.value == "") {/*do nothing*/}
 			else if ((my_form.co.value=trim_string(my_form.co.value)).search(/^[a-zA-Z]+[a-zA-Z ]*[a-zA-Z]+$/i) == -1) 
 			{
				txt +="<input type=button value=^ onClick=document.getElementById(\"co\").focus();> *Invalid country name. <br>";
				problem = true;	
			}		
 		
 		
 		
 	
		//validate email if it is entered.
		if (my_form.ph.value == "") {/*do nothing*/}
			else if((my_form.ph.value=trim_string(my_form.ph.value)).search(/^\d+[a-zA-Z0-9 \-]+\d+$/i) == -1)
				{
					txt+="<input type=button value=^ onClick=document.getElementById(\"ph\").focus();> *Invalid phone number. <br>";
					problem = true;
				}	
 		
 		
		//validate email if it is entered.
		if (my_form.em.value == "") {/*do nothing*/}
			else if((my_form.em.value=trim_string(my_form.em.value)).search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z]{2,4}$/i) == -1)
				{
					txt+="<input type=button value=^ onClick=document.getElementById(\"em\").focus();> *Invalid email address. <br>";
					problem = true;
				}

				
		 //check URL;
		if (my_form.wl.value == "") {/*do nothing*/}
			else if(!check_url(trim_string(my_form.wl.value)) || (my_form.wl.value=trim_string(my_form.wl.value)).search(/^(http:|https:|HTTP:|HTTPS:|ftp:|FTP:|www\.)/i) == -1)
				{
				txt+="<input type=button value=^ onClick=document.getElementById(\"wl\").focus();> *Invalid web link.<br>";
					problem = true;
				}		


				//check the participate length.	
 		var hp_length=my_form.hp.value.length; 
		var hp2_length=(rtrim_string(my_form.hp.value)).length;
 		
		if (my_form.hp.value == "") {/*do nothing*/}
		else if(hp_length > 5000) 
 		{
   			txt +="<input type=button value=^ onClick=document.getElementById(\"hp\").focus();> *Too much data in the How to Participate field! Please remove "+ (hp_length - 5000)+ " characters.<br>";
   			problem= true; 
 		}else if (!check_wordsize(my_form.hp.value,90)) 
 		{
   			txt +="<input type=button value=^ onClick=document.getElementById(\"hp\").focus();> *At least one single world in How to Participate field has more than 90 characters.<br>";
   			problem= true; 
 		}
	
 		
 		//check post by if it is entered.
 		if (my_form.pb.value == "") {/*do nothing*/}
 			else if ((my_form.pb.value=trim_string(my_form.pb.value)).search(/^[a-zA-Z0-9]+[a-zA-Z0-9_.@:\/ ]{4,}[a-zA-Z0-9.]+$/i) == -1) 
 			{
				txt +="<input type=button value=^ onClick=document.getElementById(\"pb\").focus();> *Invalid input in Post By field(must be more than 6 characters). <br>";
				problem = true;	
			}			//remove leading and trailing whitespace from the string 
			else {my_form.pb.value=trim_string(my_form.pb.value);}


	
		// Return true/false based upon problem.
		if (problem) 
		{
			document.getElementById("err").innerHTML=txt;
			return false;
		} 
		else { return true; }
		
		
		
	} // End of check_se_form function definition.
	

	
/*~~~~~~~~~~~~~~~~~~~~check contact form~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/	



	function check_se_cuform(my_form) {
	
	var problem = false; // Flag variable.
	var txt="";

	
			//check the name field.
		if (my_form.n.value == "") 
			{
				txt +="<input type=button value=^ onClick=document.getElementById(\"n\").focus();> *Your Name is needed. <br>";
				problem = true;	
			}
			//remove leading and trailing whitespace from the string 
			else {my_form.n.value=trim_string(my_form.n.value);}

			
				//validate email if it is entered.
		if (my_form.em.value == "") {				
				txt +="<input type=button value=^ onClick=document.getElementById(\"em\").focus();> *Your email address is needed. <br>";
				problem = true;	
			}
			else if((my_form.em.value=trim_string(my_form.em.value)).search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z]{2,4}$/i) == -1)
				{
					txt+="<input type=button value=^ onClick=document.getElementById(\"em\").focus();> *Not a valid email address. <br>";
					problem = true;
				}	
			
			
					
			
			
			//check the name field.
		if (my_form.sj.value == "") 
			{
				txt +="<input type=button value=^ onClick=document.getElementById(\"sj\").focus();> *Subject is needed. <br>";
				problem = true;	
			}
			//remove leading and trailing whitespace from the string 
			else {my_form.sj.value=trim_string(my_form.sj.value);}
	
	
	
			//check the comment field length.	
 		var me_length=my_form.me.value.length; 
		var me2_length=(trim_string(my_form.me.value)).length;
 				if (my_form.me.value == "") 
			{
				txt +="<input type=button value=^ onClick=document.getElementById(\"me\").focus();> *Message is needed. <br>";
				problem = true;	
			}

			else if(me_length > 4000) 
 		{
   			txt +="<input type=button value=^ onClick=document.getElementById(\"me\").focus();> *Too much data in the message field! Please remove "+ (me_length - 4000)+ " characters.<br>";
   			problem= true; 
 		}else if(me2_length < 20) 
 		{
   			txt +="<input type=button value=^ onClick=document.getElementById(\"me\").focus();> *No enough characters in the message field! Please add "+ (20 - me2_length)+ " characters.<br>";
   			problem= true; 
 		} 
	
	
	
			// Return true/false based upon problem.
		if (problem) 
		{
			document.getElementById("err").innerHTML=txt;
			return false;
		} 
		else { return true; }
	
	
		
	} // End of check_se_cuform function definition.
	
	/*~~~~~~~~~~~~~~~~~~~check search form~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/	
	
	function check_search_form(my_form) {
	
	var problem = false; // Flag variable.
	var txt="";
	
		
			
			//check address:city
			if (my_form.ci.value == "") 
			{
				txt +="<input type=button value=^ onClick=document.getElementById(\"ci\").focus();> *Please fill in a city name. <br>";
				problem = true;	
			}
			//remove leading and trailing whitespace from the string 
			else {my_form.ci.value=trim_string(my_form.ci.value);}	
			

	 			 //validate city
 		if (my_form.ci.value == "") {/*do nothing*/}
 			else if ((my_form.ci.value=trim_string(my_form.ci.value)).search(/^[a-zA-Z]+[a-zA-Z ]*[a-zA-Z]+$/i) == -1) 
 			{
				txt +="<input type=button value=^ onClick=document.getElementById(\"ci\").focus();> *Invalid city name. <br>";
				problem = true;	
			}		
			
	 		//validate key word
 		if (my_form.kw.value == "") {/*do nothing*/}
 			else if ((my_form.kw.value=trim_string(my_form.kw.value)).search(/^[a-zA-Z0-9]+[a-zA-Z0-9. ]*[a-zA-Z0-9.]+$/i) == -1) 
 			{
				txt +="<input type=button value=^ onClick=document.getElementById(\"kw\").focus();> *Invalid key word(s). <br>";
				problem = true;	
			}

			
				 		//validate date word
				// check input date and time.
		if ((my_form.t_m.selectedIndex == 0) || (my_form.t_m.value == "")|| (my_form.t_d.selectedIndex == 0) || (my_form.t_d.value == "")|| (my_form.t_y.selectedIndex == 0) || (my_form.t_y.value == "") ) {/*do nothing*/}
			else if (!checkdate(my_form.t_m.value, my_form.t_d.value, my_form.t_y.value)){
				txt += "<input type=button value=^ onClick=document.getElementById(\"t_m\").focus();> *Invalid date input. <br>";
				problem = true;	
			}	
			
		

	
		// Return true/false based upon problem.
		if (problem) 
		{
			document.getElementById("err").innerHTML=txt;
			return false;
		} 
		else { return true; }
		
		
		
	} // End of check_se_form function definition.