/******************************************
Module name : Js Function file
Parent module : None
Date created : 26th June 2009
Date last modified : 
Author :  Neha Sharma
Last modified by : 
Comments : The functions_js.js file contains various functions related to the web directory project.
******************************************/	


/****************COMMMOM ADMIN FUNCTION IS NEEDED FOR THE ADMIN LOGIN & LOGOUT & SETTINMG OF THE ADMIN ****************/
/******************************************
Function name : validator
Return type : boolean
Date created : 6th January 2009
Date last modified : Neha Sharma
Author : 
Last modified by : 
Comments : Function will return the true or error message after validating checkboxes
User instruction : validator(btnType)
******************************************/
function validateForm() 
{ 
	var i,p,q,nm,test,num,min,max,errors='',args=validateForm.arguments;
	j=0;
	
	var regEmail = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
	var regBlank = /[^\s]/;
	
	//var regSpace = /^([a-zA-Z0-9_\!#@^&*%~-]+)$/;
	var regSpace = /^([a-zA-Z0-9-/_!#@]+)$/;
    var regAlphaNum = /^([a-zA-Z0-9_#@]+)$/;
	var regDate = /^([0-9_]+-[0-9][0-9]+-[0-9][0-9]+)$/; 
  	var regChar = /^([a-zA-Z]+)$/;
	var regNumeric = /^([0-9]+)$/; 
	//var regDecimal = /^([0-9]+|(\.?)[0-9]+)$/;
	var regDecimal = /^([0-9]{0,20}\.?[0-9]{1,2})$/;
	
	for (i=1; i<(args.length-2); i+=3) 
	{	
		mesg=args[i+1];
		test=args[i+2]; 
		val=document.forms[""+args[0]].elements[""+args[i]];
		
		if (val) 
		{	
			
			nm=mesg; 
			noVal = val;
			val = val.value;
			if(regBlank.test(val))
			{
				if(test.indexOf('isEqual')!=-1)
				{
					result = trim(val);

					if(result.length==0)
					{
											
					errors += '- '+nm+' is required.\n'; 
					}
					else
					{
					equal_obj_val = test.substring(8,test.indexOf(":"));
					mesg_string =test.substring((test.indexOf(":")+1));

						if(val != document.forms[""+args[0]].elements[""+equal_obj_val].value)
						{ 
							errors+='- '+nm+' and '+mesg_string+' must be same.\n';
						}
					}
				}
				else if(test.indexOf('isAlphaNum')!=-1)
				{
					result = trim(val);
					if(result.length==0){
					errors += '- '+nm+' is required.\n'; 
					}else{
						if(!regAlphaNum.test(val))
						{
							errors += '- '+nm+' is not valid.\n';
						}
					}
				
				}
				else if(test.indexOf('isNumeric')!= -1)
				{
						if(!regNumeric.test(val))
						{
							errors += '- '+nm+' must contain a numeric value.\n';
							
						}
				}
				else if(test.indexOf('isDecimal') != -1)
				{
					
					if(!regDecimal.test(val))
					{
						errors += '- '+nm+' must contain a number upto 2 decimal places.\n';
					}
				}
			
				
				else if(test.indexOf('isSpace')!=-1)
				{
					result = trim(val);
					
					if(result.length==0)
					{
						errors += '- '+nm+' is required.\n'; 
					}
					else
					{
						if(!regSpace.test(val))
						{
							errors += '- '+nm+' is not valid.\n';
						}
					}
				}
				else if (test.indexOf('isEmail')!=-1) 
				{ 
					p=val.indexOf('@');
					s=val.indexOf('.');
			        if (p<1 || p==(val.length-1))
					{
						errors+='- '+nm+' must contain an e-mail Address.\n';
		
					}
					else if(!regEmail.test(val))
					{
						errors+='- '+nm+' must contain a valid e-mail Address.\n';
					}
			     }
				else if (test.indexOf('isUrl')!=-1) 
				{ 
					p=val.indexOf('http://');
					s=val.indexOf('.');
			        if (p<0 || p==(val.length-1))
					{
						errors+='- '+nm+' must be valid URL e.g. http://www.abc.com\n';
		
					}
					else if(s<p || s==(val.length-1))
					{
						errors+='- '+nm+' must be valid URL e.g. http://www.abc.com\n';
					}
			     }
				else if (test.indexOf('isChar')!=-1) 
				 { 
					var first_char;
					
					if(val.match(regChar)==null)
					{
					 	errors+='- '+nm+' must contain a character.\n';
					}
			     }
				else if(test.indexOf('isCheckbox')!=-1)//Check is check box is not checked generate error
				{	
					var valueCheckbox = noVal.checked;
					if(!valueCheckbox)
					{
						errors+='- '+'How did you come to Know about SARVESH is required.\n';
					}
				}
				else if (test.charAt(0)=='R')
				{
					result = trim(val);
					if(result.length==0){
						
					errors += '- '+nm+' is required.\n'; 
					}
				} 
			
		}
		else if (test.charAt(0)=='R')
		{
			result = trim(val);
				if(result.length==0){
					
				errors += '- '+nm+' is required.\n'; 
				}
		}
		
		 if (test.indexOf('isDate')!=-1) 
				{ 
					//alert("vineet");
					p=val.indexOf('-');
			       // alert(test.indexOf('isDate'));
			       	var sliptdate	= val.split("-");
					
					/*******************Added by rupesh Date is not before current date and month*********************/
					var today=new Date(),TY=today.getFullYear(),TM=today.getMonth(),TD=today.getDate(),TH=today.getHours();
					TM+=1;			
					if(TM<=9) 
					{	
						TM='0'+TM;
					}
					if(TD<=9)
					{
						TD='0'+TD;
					}
					/*******************Added by rupesh*********************/
					
					var sY=sliptdate[0];
					var sM=sliptdate[1];
					var sD=sliptdate[2];
					
					/*******************Added by rupesh*********************/
					//alert(TM);
				  if(sY>1)
				   {     
						
						if(sY<TY )
						{
						    errors+='- '+nm+' should be greater than current date.\n';
						}
						else if(sM==TM && sD<TD && sY==TY) 
						{ 
	
							errors+='- '+nm+' should be greater than current date.\n';
							
						}
						else if(sM<TM && sY==TY) 
						{ 
	
							errors+='- '+nm+' should be greater than current date.\n';
					    }
				   }
				
				}
		if(errors !="")
		{	if(j<=0)
			{
				focusitem = document.forms[""+args[0]].elements[""+args[i]];
				j++;
			}	
		}
		}
		
	} 
	
  if (errors)
  {
	var MasterString = getMasterString();
	alert(MasterString+'\n'+errors);
	focusitem.focus();
	return false;
   }
   else
	return true;

  document.MM_returnValue = (errors == '');
}
/******************************************
Function name : ltrim
Return type : string
Date created : 6th January 2009
Date last modified : Neha Sharma
Author : 
Last modified by : 
Comments : Function will return the main string after removing white spaces from the left
User instruction : ltrim(str)
******************************************/
function ltrim(str) { 
	for(var k = 0; k < str.length && isWhitespace(str.charAt(k)); k++);
	return str.substring(k, str.length);
}
/******************************************
Function name : rtrim
Return type : string
Date created : 6th January 2009
Date last modified : 
Author : Neha Sharma
Last modified by : 
Comments : Function will return the main string after removing white spaces from the right
User instruction : rtrim(str)
******************************************/
function rtrim(str) {
	for(var j=str.length-1; j>=0 && isWhitespace(str.charAt(j)) ; j--) ;
	return str.substring(0,j+1);
}
/******************************************
Function name : trim
Return type : string
Date created : 6th January 2009
Date last modified :
Author : Neha Sharma
Last modified by : 
Comments : Function will return the main string after removing white spaces from the right and left of the main string
User instruction : trim(str)
******************************************/
function trim(str) {
	return ltrim(rtrim(str));
}
/******************************************
Function name : isWhitespace
Return type : integer
Date created : 6th January 2009
Date last modified : 
Author : Neha Sharma 
Last modified by : 
Comments : Function will return the index of white space encounter in the string.
User instruction : isWhitespace(charToCheck)
******************************************/
function isWhitespace(charToCheck) {
	var whitespaceChars = " \t\n\r\f";
	return (whitespaceChars.indexOf(charToCheck) != -1);
}
/******************************************
Function name : checkError
Return type : boolean
Date created : 6th January 2009
Date last modified : 
Author : Neha Sharma
Last modified by : 
Comments : Function will return the true or false acording to form validation
User instruction : checkError(error)
******************************************/
function checkError(error)
{
 var flag=false;
 var MasterString = getMasterString();
 
 if(error != "")
 {
  MasterString = MasterString + error;
  flag=true;
 }
 
 if(flag == true)
 {
  alert(MasterString);
  return false;
 }
 else
  return true;
}
/******************************************
Function name : getMasterString
Return type : boolean
Date created : 6th January 2009
Date last modified : 
Author : Neha Sharma
Last modified by : 
Comments : Function will return the main string
User instruction : getMasterString()
******************************************/
function getMasterString()
{
 return "Sorry, we can not complete your request.\nKindly provide us the missing or incorrect information enclosed below.\n";
}

/*****************************
Function name : validateJobForm
Return type : boolean
Date created : 1st July 2009
Date last modified :1st July 2009 
Author : Neha Sharma
Last modified by : Neha Sharma
Comments : This is used to check validate form.
User instruction : validateComment()
************************************/
function validateJobForm(formname)
{
	var errorString = '';
	var boolfocus = false;
	var obj = document.getElementById(formname);
	var len = obj.elements.length; 
	var elementID = '';
	var flagUnCheck = 0;
	var flagCheck = 0;
	var regNumeric = /^([0-9]+)$/; 
	var regEmail = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
	var regURL = /http:\/\/([\-\w]+\.)+\w{2,3}(\/[%\-\w]+(\.\w{2,})?)*(([\w\-\.\?\\/+@&#;`~=%!]*)(\.\w{2,})?)*\/?/i;
	if(document.getElementById('frmName').value == '')
		{
			errorString += '- Full Name is required.\n';	 
			  if(!boolfocus )
			  {
				  boolfocus = true; 
				  elementID = 'frmName';	 
			  }
		}
		if(document.getElementById('frmInterest').value == '')
		{
			errorString += '- Area of Interest is required.\n';	 
			  if(!boolfocus)
			  {
				  boolfocus = true; 
				  elementID = 'frmInterest';	 
			  }
		}
		if(document.getElementById('frmMobile').value != '')
		{
			if(!regNumeric.test(document.getElementById('frmMobile').value))
			{
				errorString += '- Mobile must contain digits.\n';	 
				  if(!boolfocus)
				  {
					  boolfocus = true; 
					  elementID = 'frmMobile';	 
				  }
			}
		}
		else
		{
			errorString += '- Mobile is required.\n';	 
			  if(!boolfocus)
			  {
				  boolfocus = true; 
				  elementID = 'frmMobile';	 
			  }	
		}
		if(document.getElementById('frmEmail').value != '')
		{
			if(!regEmail.test(document.getElementById('frmEmail').value))
			{
				errorString += '- Email must contain valid email address.\n';	 
				  if(!boolfocus)
				  {
					  boolfocus = true; 
					  elementID = 'frmEmail';	 
				  }
			}
		}
		else
		{
			errorString += '- Email is required.\n';	 
			  if(!boolfocus)
			  {
				  boolfocus = true; 
				  elementID = 'frmEmail';	 
			  }	
		}
		for(i=0;i<len;i++) 
		{
			if(obj.elements[i].type=='checkbox')
			{
				if(obj.elements[i].checked && obj.elements[i].name == 'frmSource[]')
				{
					if(obj.elements[i].checked && obj.elements[i].value == 'OtherWebsite')
					{
						flagUnCheck = 1;
						if(document.getElementById('frmOtherWebsite').value == '')
						{
							errorString += '- Please Specify the other website.\n';
							flagUnCheck = 1;
							if(!boolfocus)
						  	{
							  boolfocus = true; 
							  //elementID = 'frmOtherWebsite';
						  	}	
						}
						else
						{
							if(!document.getElementById('frmOtherWebsite').value.match(regURL))
							{
								errorString += '- Other Website must contain a valid URL e.g. http://www.abc.com\n';	 
								flagUnCheck = 1;
								 if(!boolfocus)
								  {
									  boolfocus = true; 
									  //elementID = 'frmOtherWebsite';
								  }
							}
						}
					}
					else
					{
						flagUnCheck = 1;	
					}
				}
				else
				{
					flagCheck = 1;
				}
			}
		}
		if(flagCheck && flagUnCheck == 0)
		{
			errorString += '- How did you come to Know about SARVESH is required.\n';
			if(!boolfocus)
			{
			  boolfocus = true; 
			}
		}
		if(document.getElementById('frmResume').value == '')
		{
			errorString += '- Resume is required.\n';	 
			  if(!boolfocus )
			  {
				  boolfocus = true; 
				  elementID = 'frmResume';	 
			  }
		}
		if(errorString == '')
		{
			return true;
		}
		else
		{	
			if(boolfocus)
			{
				if(elementID != '')
				 {
				 	document.getElementById(elementID).focus();
				 }
			}
			masterString =  getMasterString();
			alert(masterString+'\n'+errorString);
			return false;
		}
s}



