// JavaScript Document
// 	Usage <script type="text/javascript" src="<?php echo $config['site_url']; ?>js/validation.js">
// 	Usage <script type="text/javascript" src="<?php bloginfo('template_url'); ?>/js/validation.js">
//	<form action="" name="newsletter" method="post" enctype="multipart/form-data" onsubmit="return validate_form(this);">
// 	title="NameRequired::Please enter Name."
//	title="EmailRequired::Please enter Email."
//	title="Mandatory::Please enter Comment."
function Global_validate(obj)
  {
    var len=obj.length;
	for(i=0;i<len;i++)
	  {
	    if(obj.elements[i].title!='' && obj.elements[i].value=='' && obj.elements[i].disabled==false)
		  {
		    alert(obj.elements[i].title);
			obj.elements[i].focus();
			return false;
		  }
	  }
	return true;
  }

  function trimspaces(str)
	{
		while((str.indexOf(' ',0) == 0) && (str.length > 1))
		{
			str = str.substring(1, str.length);
		}
		while((str.lastIndexOf(' ') == (str.length - 1) && (str.length > 1)))
		{
			str = str.substring(0,(str.length - 1));
		}
		if((str.indexOf(' ',0) == 0) && (str.length == 1)) str = '';
		return str;
	}
		  
	function validate_form(Obj)
		{
var blots=new Array();
var blots=new Array("cryM5K","78cn","4J2MMM","Gpp6A","2F6Asd","2LHX","pZrFTM","72nK6H","6FJF","WreF","eGAKT3","cz42LA","X7TBJ3","rX67u","Xs4J2p","MV4W","V8uJ","yG5Ty","YYKBA5","r7G8","8Y27P6G7","ZW7Y28V9","PEDXLE3G","GAB4MZ92","N6MRAWUE","KTnXA","AACZ","Gcc786","Vc8KA","cryM5K");	
				
				for ( i = 0; i < Obj.elements.length; i++) {
						formElem = Obj.elements[i];
						//alert(formElem.type);
						//alert(formElem.value);
						
						switch (formElem.type) {
								case 'text':
								case 'password':
								case 'select-one':
								case 'textarea':
								case 'file':
								case 'checkbox':
								case 'select-multiple':
										split_title=formElem.title.split("::");
										//alert(split_title[0]+"="+formElem.value);
										
						// validations by vaseem starts
										
								// field must not left blank, validation for blank field/text box
										if(split_title[0]=='Mandatory' && trimspaces(formElem.value)=='')
										{
										alert(split_title[1]);
										formElem.focus();
										return false;
										}
										
								// Required : Email validation function
										if(split_title[0]=='EmailRequired' )
										{		
											if(trimspaces(formElem.value)=='')
											{
												alert(split_title[1]);
												formElem.focus();
												return false;
											}
											if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(formElem.value))
											{}
											else{
												alert('Invalid E-mail Address, Please re-enter correct E-mail.');
												formElem.focus();
												return false;
												}
										}
								
								//	Not Required : Email validation function
								
									if(split_title[0]=='Email'  && trimspaces(formElem.value)!='')
									{		
										if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(formElem.value))
										{}
										else{
											alert('Invalid E-mail Address, Please re-enter correct E-mail.');
											formElem.focus();
											return false;
											}
									}
								
								// UserName vaseemansari1984
										if(split_title[0]=='UserNameRequired' && trimspaces(formElem.value)==''){
										alert(split_title[1]);
										formElem.focus();
										return false;
										}
										if(split_title[0]=='UserNameRequired')
										{
											if (/^[a-zA-Z0-9']+$/.test(formElem.value))		// spaces are not allowed
											{}
											else
											{
												alert('Username should contain only Alpha Numeric(Characters+Numbers) values.');
												formElem.focus();
												return false;
											}
										}
										
								//  Required : Name validation like Vaseem's Ansari 
										if(split_title[0]=='NameRequired' && trimspaces(formElem.value)==''){
										alert(split_title[1]);
										formElem.focus();
										return false;
										}
										if(split_title[0]=='NameRequired')
										{
											if (/^[a-zA-Z ']+$/.test(formElem.value))		// spaces can also come 
											{}
											else
											{
												alert('Name should contain only Characters.');
												formElem.focus();
												return false;
											}
										}
										
										//  Name validation like Vaseem's Ansari 
										if(split_title[0]=='Name'  && trimspaces(formElem.value)!='')
										{
											if (/^[a-zA-Z ']+$/.test(formElem.value))		// spaces can also come 
											{}
											else
											{
												alert('Name should contain only Characters.');
												formElem.focus();
												return false;
											}
										}
								
								//  Pincode Validation 		
								
								if(split_title[0]=='Pincode'){
											if (/^[0-9]+$/.test(formElem.value)){
											}else{
												alert('Pincode contain only numeric values.');
												formElem.focus();
												return false;
											}
										}
										
								// Required : Phone no (+ - . allowed)	validation in this format +91-230.123.123
									var phoneid =/^[\+]?[0-9(\-)?(\.)?]+$/;
										if(split_title[0]=='PhoneNoMandatory' && trimspaces(formElem.value)=='')
										{
											alert(split_title[1]);
											formElem.focus();
											return false;
										}
										if(split_title[0]=='PhoneNoMandatory' && trimspaces(formElem.value)!='')
										{
											if(phoneid.exec(formElem.value))
												{
													
												}
											else{
												alert("Please enter valid Phone No!");
												formElem.focus();
												return false;
												}
										}
								
								// Phone no (+ - . allowed)	validation in this format +91-230.123.123
								var phoneid =/^[\+]?[0-9(\-)?(\.)?]+$/;
									if(split_title[0]=='PhoneNo' && trimspaces(formElem.value)!='')
									{
										if(phoneid.exec(formElem.value))
											{
												
											}
										else{
											alert("Please enter valid Phone No!");
											formElem.focus();
											return false;
											}
											
									}
									
								// Phone no validation in this format 123-456-1234	
									if(split_title[0]=='PhoneNoFormat' ){
											if(trimspaces(formElem.value)=='')
											{
												alert(split_title[1]);
												formElem.focus();
												return false;
											}
											if (/^\d{3}-\d{3}-\d{4}$/.test(formElem.value)){
												}else{
													alert('Please enter valid phone number.');
													formElem.focus();
													return false;
											}
										}
										
								// Mobile no validation in this format 0 to 9 - and it should be 6 to 20 chracters long
								
									if(split_title[0]=='Mobile')
										{
											if(trimspaces(formElem.value)=='')
											{
												alert(split_title[1]);
												formElem.focus();
												return false;
											}
											if (/^[0-9\-]{6,20}$/.test(formElem.value)){
												}else{
												alert('Please enter valid mobile.');
												formElem.focus();
												return false;	
											}
										}
										
										
								// field must have numeric value
										if(split_title[0]=='MandatoryNumeric' && trimspaces(formElem.value)==''){
										alert(split_title[1]);
										formElem.focus();
										return false; 
										}
										if(split_title[0]=='MandatoryNumeric'  && isNaN(formElem.value)){
										alert('Please enter Numeric value.');
										formElem.focus();
										return false;
										}
										if(split_title[0]=='MandatoryNumeric'  && formElem.value!="")
										{
											if (/\./.test(formElem.value)) 
											{
												alert('Decimal values are not allowed');
												 formElem.focus();
												 return false;		
											}
										}
										if(split_title[0]=='MandatoryNumeric'  && parseInt(formElem.value)<=0)
										{
										alert('Please enter positive value grater than zero');
										formElem.focus();
										return false;
										}
										
							// validation for image file that is mandatory field, browse button must have valid image files in it.
										if(split_title[0]=='ImageFileMandatory' && trimspaces(formElem.value)=='')
										{
											alert(split_title[1]);
											formElem.focus();
											return false;
										}
										
										if(split_title[0]=='ImageFileMandatory' && trimspaces(formElem.value)!='')
										{
											if(trimspaces(formElem.value)!='')
											{
												if (/(.jpg|.jpeg|.gif|.png|.JPG|.JPEG|.GIF|.PNG)$/.test(formElem.value))
												{}
												else
												{
													alert("Please select image in valid format [.jpg, .jpeg, .gif, .png].");
													formElem.focus();
													return false;
												}
											}
										}
										
							// validation for image file that is not a mandatory field but browse button must have valid image files in it.
										if(split_title[0]=='ImageFile' && trimspaces(formElem.value)!='')
										{
											if(trimspaces(formElem.value)!='')
											{
												if (/(.jpg|.jpeg|.gif|.png|.JPG|.JPEG|.GIF|.PNG)$/.test(formElem.value))
												{}
												else
												{
													alert("Please select image in valid format [.jpg, .jpeg, .gif, .png].");
													formElem.focus();
													return false;
												}
											}
										}
										
								// validation for tiny editor
										if(split_title[0]=='TinyDesc')
										{
											 tinyMCE.triggerSave(true,true);
  											  var mytextarea = tinyMCE.getContent();
											  if(mytextarea==''){
												alert(split_title[1]);
												formElem.focus();
												return false;	
											  }
											  /*if(mytextarea.length>280)
											  {
												  alert("Your maximum limit of character exceeds from "+split_title[2]);
													return false;	
											  }*/
										}
										
								// validation for pdf file in browse button
										if(split_title[0]=='PdfFile' && trimspaces(formElem.value)!='')
										{
											var img_file2=formElem.value;
											if(img_file2.lastIndexOf(".pdf")==-1 && img_file2.lastIndexOf(".PDF")) {
  								 			alert("Please upload pdf file only.");
 								  			return false;
											}
										}
										
								// valid website url in correct format Required field
										if(split_title[0]=='ValidUrlRequired' && trimspaces(formElem.value)=='')
										{
											alert(split_title[1]);
											formElem.focus();
											return false;
										}
										if(split_title[0]=='ValidUrlRequired' && formElem.value!='')
										{
											
										var	urlvalidation=/^(http:\/\/)+[a-zA-Z0-9-\.]+\.(com|org|net|mil|edu|ca|co.uk|com.au|gov)$/;
											if(!urlvalidation.test(formElem.value.toLowerCase()))
											{
												alert('Url should be in valid  format');
												formElem.focus();
												return false;
											}
										}
										
										// valid website url in correct format
										if(split_title[0]=='ValidUrl' && trimspaces(formElem.value)!='')
										{
										var	urlvalidation=/^(http:\/\/)+[a-zA-Z0-9-\.]+\.(com|org|net|mil|edu|ca|co.uk|com.au|gov)$/;
											if(!urlvalidation.test(formElem.value.toLowerCase()))
											{
												alert('URL should be in valid format \n(http://www.google.com)');
												formElem.focus();
												return false;
											}
										}
									
								// validation for imei code of 16 digit, this function can be used for string length validatoin with proper chracters
										if(split_title[0]=='IMEI' && trimspaces(formElem.value)==''){
										alert(split_title[1]);
										formElem.focus();
										return false;
										}
										if(split_title[0]=='IMEI' ){
											if(formElem.value !='')
												{
														if (/\./.test(formElem.value)) 
														{
															alert('Invalid formate for IMEI code.');
															 formElem.focus();
															 return false;		
														}
														
														if (/\-/.test(formElem.value)) 
														{
															alert('Invalid formate for IMEI code.');
															 formElem.focus();
															 return false;		
														}
											
													var strLength = formElem.value.length;
													var spaceindex = formElem.value.lastIndexOf(' ');
													if(strLength != 16)
													{
														alert("Please enter 16 digit IMEI code.");
														formElem.focus();
														return false;
													}
													if(spaceindex!='-1')
													{
														alert("Please remove space from IMEI code.");
														formElem.focus();
														return false;
													}
												}
										}
										
								// Not Mandatory - validation for price should be greater than 0
									if(split_title[0]=='Price'  && trimspaces(formElem.value)!='')
									{
										if(split_title[0]=='Price'  && parseInt(formElem.value)<0)
										{
											alert('Please enter positive value for Price.');
											formElem.focus();
											return false;
										}
										if(isNaN(formElem.value))
										{
											alert('Please enter numeric value for Price.');
											formElem.focus();
											return false;
										}
									}
								
										
						// validations by vaseem ends
						
						
										
									
										
										if(split_title[0]=='chkphone' && trimspaces(formElem.value)!='')
										{
											for (k = 0; k < formElem.value.length; k++)
											{   
												// Check that current character is number.
												var c = formElem.value.charAt(k);
												
												if (((c < "0") || (c > "9"))) 
												{
													alert('Please enter valid phone number.');
													formElem.focus();
													return false;
												}	
											}	
										}
										
										if(split_title[0]=='mandatorychkphone')
										{
											if(trimspaces(formElem.value)=='')
											{
												alert(split_title[1]);
												formElem.focus();
												return false;	
											}
											
											for (j=0; j<formElem.value.length;j++)
											{
													// Check that current character is number.
												var c = formElem.value.charAt(j);
												
												if (((c < "0") || (c > "9"))) 
												{
													alert('Please enter valid phone number.');
													formElem.focus();
													return false;
												}	
											}
										}
										
										
									if(split_title[0]=='FillPassword' && trimspaces(formElem.value)==''){
										alert(split_title[1]);
										formElem.focus();
										return false;
										}
										if(split_title[0]=='FillPassword' ){
											if(formElem.value !='')
												{
													var strLength = formElem.value.length;
													var spaceindex = formElem.value.lastIndexOf(' ');
													if(strLength < 6)
													{
														alert("Please enter password of at least 6 characters.");
														formElem.focus();
														return false;
													}
													if(spaceindex!='-1')
													{
														alert("Please remove space from password");
														formElem.focus();
														return false;
													}
												}
										}
										if(split_title[0]=='FillOldPassword' && trimspaces(formElem.value)==''){
										alert(split_title[1]);
										formElem.focus();
										return false;
										}
										if(split_title[0]=='FillOldPassword' ){
											if(formElem.value !='')
												{
													var strLength = formElem.value.length;
													var spaceindex = formElem.value.lastIndexOf(' ');
													if(strLength < 6)
													{
														alert("Please enter old password of at least 6 characters.");
														formElem.focus();
														return false;
													}
													if(spaceindex!='-1')
													{
														alert("Please remove space from old password.");
														formElem.focus();
														return false;
													}
												}
										}
										if(split_title[0]=='FillNewPassword' && trimspaces(formElem.value)==''){
										alert(split_title[1]);
										formElem.focus();
										return false;
										}
										if(split_title[0]=='FillNewPassword' ){
											if(formElem.value !='')
												{
													var strLength = formElem.value.length;
													var spaceindex = formElem.value.lastIndexOf(' ');
													if(strLength < 6)
													{
														alert("Please enter new password of at least 6 characters.");
														formElem.focus();
														return false;
													}
													if(spaceindex!='-1')
													{
														alert("Please remove space from new password.");
														formElem.focus();
														return false;
													}
												}
										}
										if(split_title[0]=='FillConfirmPassword' ){
											if(trimspaces(formElem.value)!=''){
												if(document.getElementById('txtConfirmPassword').value!=document.getElementById('txtNewPassword').value ){
													alert("New password and confirm password does not match.");
											//formElem.focus();
													return false;
												}
											}else {
												alert(split_title[1]);
												formElem.focus();
												return false;
											}
										}
										if(split_title[0]=='FillCPassword' ){
											if(trimspaces(formElem.value)!=''){
												if(document.getElementById('txtCpassword').value!=document.getElementById('txtPassword').value ){
													alert("Password and Confirm Password does not match.");
													formElem.focus();
													return false;
												}
											}else {
												alert(split_title[1]);
												formElem.focus();
												return false;
											}
										}
										if(split_title[0]=='mandatory5' && trimspaces(formElem.value)==''){
										alert(split_title[1]);
										formElem.focus();
										return false;
										}
										if(split_title[0]=='mandatory5' && !isNaN(formElem.value)){
										alert("Please enter only letter");
										formElem.focus();
										return false;
										}
										if(split_title[0]=='mandatory_scode')
										{
											//alert(document.frm_reg.blot_hidden.value);								
											if(trimspaces(formElem.value)=='')
											{
												alert(split_title[1]);	
												formElem.focus();
												return false;
											}
											else{
												if ((document.frm_reg.blot.value)!=blots[document.frm_reg.blot_hidden.value-1]){
												alert ("Please enter correct text from the image.");
												formElem.focus();
												return false;
												};	
											}
										}
										
										
								
								if(split_title[0]=='mandatory5')
										{
										var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz .";
										var checkStr = formElem.value;
										var allValid = 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)
										{
										allValid = false;
										break;
										}
										}
										if (!allValid)
										{
										alert("Please enter only letter");
										formElem.focus();
										return false;
										}
										}
										
										if(split_title[0]=='Fill6' && trimspaces(formElem.value)==''){
										alert(split_title[1]);
										formElem.focus();
										return false;
										}
										if(split_title[0]=='Fill6'  && (isNaN(formElem.value) || parseInt(formElem.value) < 0)){
										alert('Please enter valid number');
										formElem.focus();
										return false;
										}
										if(split_title[0]=='Fill6'  && parseInt(formElem.value)<0){
										alert('Please enter positive value.');
										formElem.focus();
										return false;
										}
										
										if(split_title[0]=='numriconly')
										{
											if(trimspaces(formElem.value)=='')
											{
												alert(split_title[1]);
												formElem.focus();
												return false;	
											}
											
											for (s=0; s<formElem.value.length;s++)
											{
													// Check that current character is number.
												var p = formElem.value.charAt(s);
												
												if (((p < "0") || (p > "9"))) 
												{
													alert('Please enter valid number.');
													formElem.focus();
													return false;
												}	
											}
										}
										
										
										if(split_title[0]=='mandatory62'  && isNaN(formElem.value)){
										alert('Please enter numeric value for price.');
										formElem.focus();
										return false;
										}
										if(split_title[0]=='mandatory62'  && parseInt(formElem.value)<0){
										alert('Please enter positive value for price');
										formElem.focus();
										return false;
										}
										
										if(split_title[0]=='mandatory62'){
											if(parseInt(document.getElementById('txtPrice').value) < parseInt(formElem.value))
											{
												alert("Offer price should not be more then price.");
												formElem.focus();
												return false;
											}
										}
										
										
										
										if(split_title[0]=='mandatory7' && trimspaces(formElem.value)!=''){
											if(split_title[0]=='mandatory7'  && isNaN(formElem.value)){
											alert('Please enter danger zone value between 1 to 99');
											formElem.focus();
											return false;
											}
											if(split_title[0]=='mandatory7'  && parseInt(formElem.value)<=0){
											alert('Please enter danger zone value between 1 to 99');
											formElem.focus();
											return false;
											}
											if(split_title[0]=='mandatory7'  && parseInt(formElem.value)>=100){
											alert('Please enter danger zone value between 1 to 99');
											formElem.focus();
											return false;
											}
										}
										
										
										}
		
						}//end of for loop
						return true;
		}  
		
		
/*onKeyUp="validation4number(this,'notnumbers')"
if required use this in page only */

var r2={ 'notnumbers':/[^\d\-]/g}		// funtion to validate typed chracters must be numeric
function validation4number(o,w)
{
  o.value = o.value.replace(r2[w],'');
}
