// JavaScript Document
//Created by Sandip Patel
var ext;
function _validate()
{
	var arg = arguments;	
	var formName = arg[0];
//	alert(arg.length);
	for(i=1;i<arg.length;i+=3)
	{
		var id = arg[i];
		var para = arg[i+1];
		var msg = arg[i+2];
//		alert(i);
		switch(para)
		{
			case "validateBlank":  // for validating blank 			                      
								 if(!validateBlank(id,msg))
										return; 
									break;
			case "validateRadio" : // for validation of Radio button whether checked or not
								 if(!validateRadio(id,msg))
										return; 
									break;

			case "validateCheckBox" : // for validation of Check box whether checked or not
								 if(!validateCheckBox(id,msg))
										return; 
									break;
			case "validateCheckBoxGroup" :// for validation of Check boxes whether one check box is checked or not
									 if(!validateCheckBoxGroup(id,msg))
										return; 
									break; 									
			case "validatePass" : // for validation password and confirm password are same or not
								  // in this pass both controls name by conatinating '#' both field name 
								  // e.g. one text box name is 'pwd'  and other is 'cnfPwd' then pass 'pwd#cnfPwd'
								 if(!validatePass(id,msg))
										return; 
									break;									
			case "validateEmail" : // for validating email id
								 if(!validateEmail(id,msg))
										return; 
									break;									
			case "validateUrl" : // for validating url
								 if(!validateUrl(id,msg))
										return; 
									break;				
			case "validateFile" : // for validating File Extension
								 if(!validateFile(id,msg))
										return; 
									break;
			case "validateNumber" : // for validating File Extension
								 if(!validateNumber(id,msg))
										return; 
									break;									
						
		}// switch case ends here....
	}	//for loop ends here
	return true;
}
function validateBlank(id,msg)
{	if(trim(document.getElementById(id).value)=="")
	{
		alert(msg);
		document.getElementById(id).value = trim(document.getElementById(id).value);
		document.getElementById(id).focus();
		return false;
	}
	else
	{
			var controlType = document.getElementById(id).type;
			controlType = controlType.toLowerCase();
//			alert(controlType);
			if(controlType != "file" && controlType != "select-one" && controlType != "select-multiple") 
				document.getElementById(id).value = trim(document.getElementById(id).value);

			return true;
	}
}
function validateRadio(id,msg)
{
	var rObj = document.getElementsByName(id)	
	var flg = 0;
	for(j=0;j<rObj.length;j++)
	{
		if(rObj[j].checked)
			flg++;
	}
	if(!flg)
	{
		alert(msg);		
		return false;
	}
	else
		return true;
	
}
function validateCheckBox(id,msg)
{
	
	var chkObj = document.getElementById(id);	
//	alert(chkObj.checked);
	if(!chkObj.checked)
	{	
		alert(msg);		
		return false;
	}
	else
		return true;
	
}
function validateCheckBoxGroup(id,msg)
{
	var rObj = document.getElementsByName(id)	
	var flg = 0;
	for(j=0;j<rObj.length;j++)
	{
		if(rObj[j].checked)
			flg++;
	}
	if(!flg)
	{
		alert(msg);		
		return false;
	}
	else
		return true;
	
}
function validatePass(id,msg)
{
	var pwdArr;
	pwdArr = id.split("#");
	var pwd = document.getElementById(pwdArr[0]).value;
	var cnfpwd = document.getElementById(pwdArr[1]).value;
	if(pwd != cnfpwd)
	{	
		alert(msg);		
		document.getElementById(pwdArr[1]).focus();
		return false;
	}
	else
		return true;
	
}
function validateEmail(id,msg)
{
	var email = document.getElementById(id);
	 var str=trim(email.value);
	 var filter=/^.+@.+\..{2,3}$/ 
	 if (filter.test(str))
	 {
		 return true;
	  }
	else
	 { 
	 	alert(msg);
		email.focus();
		return false;
	 }	
}
function validateUrl(id,msg){
var theUrl = document.getElementById(id).value;
  if(theUrl.match(/^w+([\.\-]\w+)*\.\w{2,4}(\:\d+)*([\/\.\-\?\&\%\#]\w+)*\/?$/i) ||
     theUrl.match(/^mailto\:\w+([\.\-]\w+)*\@\w+([\.\-]\w+)*\.\w{2,4}$/i)){
    return true; // url is valid
  } else {
	  alert(msg);
	  document.getElementById(id).focus();
    return false; // url is not valid
  }
}
/*
//	validation with regular expression

function validateFile(id,msg)
{
	var fileObj = document.getElementById(id);	
	var extVal = GetExt(fileObj.value);	
	var path = fileObj.value;
	var str = "";

	for(i=0;i<ext.length;i++)
	{
		var bar = i+1 != ext.length ? "|":"";	
	//	alert(bar);
		str += "(\."+ext[i]+")" + bar;
	}
	//alert(str);
	RgExp= new RegExp(str+"$");

	if (!RgExp.test(path))
	{
		alert(msg);
		return false;
	}

	return true;
		
//	RgExp= new RegExp("(\.png)|(\.jpg)|(\.gif)|(\.jpe)|(\.jpeg)|(\.bmp)$");
}*/
function validateFile(id,msg)
{
	var fileObj = document.getElementById(id);	
	var extVal = GetExt(fileObj.value);	
	var path = fileObj.value;
	var str = "";
strval = document.frmCV.cv.value;
				extn = path.substr(path.lastIndexOf("."));
				
//				validate_str = document.frmCV.allowed_file.value;
				validate_str  = ext
				//alert(ext);
				if(validate_str.indexOf(extn)<0)
				{
					alert(msg)
					return false;
				}
	/*for(i=0;i<ext.length;i++)
	{
		var bar = i+1 != ext.length ? "|":"";	
	//	alert(bar);
		str += "(\."+ext[i]+")" + bar;
	}
	//alert(str);
	RgExp= new RegExp(str+"$");

	if (!RgExp.test(path))
	{
		alert(msg);
		return false;
	}
*/
	return true;
		
//	RgExp= new RegExp("(\.png)|(\.jpg)|(\.gif)|(\.jpe)|(\.jpeg)|(\.bmp)$");
}
function validateNumber(id,msg)
{
     var obj = document.getElementById(id);
	 if (isNaN(trim(obj.value)) || trim(obj.value) == "")
	 {
	 	alert(msg);
		obj.focus();
		return false;
	  }
	else
	 { 
		return true;
	 }		
}
function trim(s) 
{
	// Remove leading spaces and carriage returns
	while ((s.substring(0,1) == ' ') || (s.substring(0,1) == '\n') || (s.substring(0,1) == '\r'))
	{ s = s.substring(1,s.length); }
	
	// Remove trailing spaces and carriage returns
	while ((s.substring(s.length-1,s.length) == ' ') || (s.substring(s.length-1,s.length) == '\n') || (s.substring(s.length-1,s.length) == '\r'))
	{ s = s.substring(0,s.length-1);}
	
	return s;
}
// for only characters in text field
function charsOnly(field, event)
{
 var key,keychar;

 if (window.event)
   key = window.event.keyCode;
 else if (event)
    key = event.which;
 else
    return true;
 
 keychar = String.fromCharCode(key);

var valid_str="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ";
  // then check for the numbers 

 if ((key==null) || (key==0) || (key==8) || 
     (key==9) || (key==13) || (key==27) )
   return true;

  else if ((valid_str.indexOf(keychar) > -1))
         {
           window.status = "";
           return true;
         }
       else
         {
           window.status = "Field accepts Alphabetical characters only"; 
           return false;
         }
	return false;	 
 }

// for only numbers in text field
function numOnly(field, event)
{
 var key,keychar;

 if (window.event)
   key = window.event.keyCode;
 else if (event)
    key = event.which;
 else
    return true;
 
 keychar = String.fromCharCode(key);

var valid_str="0123456789";
  // then check for the numbers 

 if ((key==null) || (key==0) || (key==8) || 
     (key==9) || (key==13) || (key==27) )
   return true;

  else if ((valid_str.indexOf(keychar) > -1))
         {
           window.status = "";
           return true;
         }
       else
         {
           window.status = "Field accepts Alphabetical characters only"; 
           return false;
         }
	return false;	 
 }
