// JavaScript Document
/***********************************************************************************************************
*  *  *  *  *  *  *  *  *  *  *   Synergy   Education Pvt Ltd    *  *  *  *  *  *  *  *   *
************************************************************************************************************
* Filename				: validation_functions.js
* Description 		  	: Functions are defined for validating the html form fields

* Modification Log
* 	Date                	 Author                       		Description
* ---------------------------------------------------------------------------------------------------------
* 29-April-2009				Mahendra Kumar Patidar						Created the file.
*
**********************************************************************************************************/

//remove the white sapces from the beginning & end of the string
function trim(str)
{
   return str.replace(/^\s*|\s*$/g,"");
}

//This is the JS function to validate whether the input is not a empty, 
//if the input is non empty then it will passed to further process else the required input will be ask to the end user
function fieldBlank(fieldName,msg)
{
	if(trim(fieldName.value)=="")
	{
		alert(msg);
		fieldName.focus();
		return false;
	}
	else
	{
		return true;
	}		
}

//This is the JS function to validate whether the input is a valid numeric characters, 
//if the input is valid it will be passed to further process else the required input will be ask to the end user
function validNumeric(fieldName,msg)
{
	if(trim(fieldName.value)!="")
	{	
		var objRegExp  =  /(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/; 
		if(objRegExp.test(fieldName.value)==true)
		{
			return true;
		}
		else
		{
			alert(msg);
			fieldName.select();
			return false;
		}
	}
	return true;	
}

//This is the JS function to validate whether the input is a valid email format, 
//if the input is valid it will be passed to further process else the required input will be ask to the end user
function validEmail(fieldName,msg)
{
	if(trim(fieldName.value)!="")
	{	
		var email = fieldName.value;
		var matcharray = email.match(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z]+)*\.[A-Za-z]+$/) 
		if(matcharray==null)
		{	
			alert(msg);
			fieldName.select();
			return false;
		}
		else 
		{
			return true;
		}
	}
	return true;		
}

//This is the JS function to validate whether the input is a valid alphabetic characters, 
//if the input is valid it will be passed to further process else the required input will be ask to the end user
function validAlpha(fieldName,msg)
{
	if(trim(fieldName.value)!="")
	{	
		var reAlpha = /^[a-zA-Z ]+$/;	
		if(reAlpha.test(fieldName.value)==true)
		{
			return true;
		}
		else
		{
			alert(msg);
			fieldName.select();
			return false;
		}
	}
	return true;	
}

//This is the JS function to validate whether the input is a valid alphanumeric characters, 
//if the input is valid it will be passed to further process else the required input will be ask to the end user
function validAlphaNumeric(fieldName,msg)
{
	if(trim(fieldName.value)!="")
	{	
		var reAlphanumeric = /^[a-zA-Z0-9_ ]+$/;
		if(reAlphanumeric.test(fieldName.value)==true)
		{
			return true;
		}
		else
		{
			alert(msg);
			fieldName.select();
			return false;
		}	
	}
	return true;
}

//This is the JS function to validate whether the input is a valid URL Format, 
//if the input is valid it will be passed to further process else the required input will be ask to the end user
function validURL(fieldName,msg)
{
	if(trim(fieldName.value)!="")
	{	
		str = "http://"+trim(fieldName.value);
		var re;
		re = new RegExp("(http|ftp|https)://[-A-Za-z0-9_/]+[.][-A-Za-z0-9._/]+");
		if(re.test(str)==false)
		{			
			alert(msg);
			fieldName.select();
			return false;
		}
		posOfAtSign = str.indexOf(".")
		if (posOfAtSign == -1)
		{		
			alert(msg);
			fieldName.select();
			return false;
		}
	}
	return true;
}

//This is the JS function to validate whether the input is a valid Domain Name, 
//if the input is valid it will be passed to further process else the required input will be ask to the end user
function validDomain(fieldName,msg)
{	
	var strURL = trim(fieldName.value);
	if(strURL!="")
	{
		var is_protocol_ok=strURL.indexOf('www');
		var is_dot_ok = strURL.indexOf('.');
		if (((is_protocol_ok==-1) && (is_dot_ok!=-2)) || ((is_protocol_ok!=-1) && (is_dot_ok==-1)))
		{ 
			alert(msg+"\n www.test.com");
			fieldName.focus();
			return false;
		}	
	}
	return true;
}

//This is the JS function to validate whether the input is a valid Phone Number or Fax Number, 
//if the input is valid it will be passed to further process else the required input will be ask to the end user
function validPhoneFax(fieldName,msg)
{
	if(trim(fieldName.value)!="")
	{	
		var regexp=/^(\d{3}-\d{3}-\d{4}|\d{10}|\(\d{3}\)\d{3}-\d{4}|\d{6}|\d{7}|\d{8})$/;
		x = trim(fieldName.value);
		if(!(regexp.test(x)))
		{
			var str = "";
			str = str + msg;
			str = str +  "\n The correct forms are : ";
			str = str + "\n xxx-xxx-xxxx";
			str = str + "\n (xxx)xxx-xxxx";
			str = str + "\n xxxxxxxxxx (10 digits no)";
			str = str + "\n xxxxxx (6 digits no)";
			str = str + "\n xxxxxxx (7 digits no)";
			str = str + "\n xxxxxxxx (8 digits no)";
			alert(str);
			fieldName.select();
			return false; 
		}
	}
	return true;
}

//This is the JS function to validate whether the alphabetic characters input within the specified length, 
//if the input is valid it will be passed to further process else the required input will be ask to the end user
function validAlphaLength(fieldName,msg,start,end)
{	
	if(trim(fieldName.value)!="")
	{	
		if(validAlpha(fieldName,'Please enter the only alphabetic characters')==true)
		{
			if(end!=null && start!=null)
			{
				if(fieldName.value.length>=start && fieldName.value.length<=end)
				{
					return true;
				}
				else
				{
					alert(msg);
					fieldName.select();
					return false;				
				}
			}
			else if(end!=null && start==null)
			{
				if(fieldName.value.length<=end)
				{
					return true;
				}
				else
				{
					alert(msg);
					fieldName.select();
					return false;				
				}
			}
			else
			{
				if(fieldName.value.length>=start)
				{
					return true;
				}
				else
				{
					alert(msg);
					fieldName.select();
					return false;				
				}
			}		
		}
		else
		{			
			return false;
		}
	}
	return true;
}

//This is the JS function to validate whether the alphanumeric characters input within the specified length, 
//if the input is valid it will be passed to further process else the required input will be ask to the end user
function validAlphaNumericLength(fieldName,msg,start,end)
{	
	if(trim(fieldName.value)!="")
	{	
		if(validAlphaNumeric(fieldName,'Please enter only alphanumeric characters')==true)
		{
			if(end!=null && start!=null)
			{
				if(fieldName.value.length>=start && fieldName.value.length<=end)
				{
					return true;
				}
				else
				{
					alert(msg);
					fieldName.select();
					return false;				
				}
			}
			else if(end!=null && start==null)
			{
				if(fieldName.value.length<=end)
				{
					return true;
				}
				else
				{
					alert(msg);
					fieldName.select();
					return false;				
				}
			}
			else
			{
				if(fieldName.value.length>=start)
				{
					return true;
				}
				else
				{
					alert(msg);
					fieldName.select();
					return false;				
				}
			}		
		}
		else
		{			
			return false;
		}	
	}
	return true;
}

//This is the JS function to validate whether the Numeric characters input within the specified length, 
//if the input is valid it will be passed to further process else the required input will be ask to the end user
function validNumericLength(fieldName,msg,start,end)
{
	if(trim(fieldName.value)!="")
	{		
		if(validNumeric(fieldName,'Please enter only numeric characters')==true)
		{
			if(end!=null && start!=null)
			{
				if(fieldName.value.length>=start && fieldName.value.length<=end)
				{
					return true;
				}
				else
				{
					alert(msg);
					fieldName.select();
					return false;				
				}
			}
			else if(end!=null && start==null)
			{
				if(fieldName.value.length<=end)
				{
					return true;
				}
				else
				{
					alert(msg);
					fieldName.select();
					return false;				
				}
			}
			else
			{
				if(fieldName.value.length>=start)
				{
					return true;
				}
				else
				{
					alert(msg);
					fieldName.select();
					return false;				
				}
			}		
		}
		else
		{			
			return false;
		}
	}
	return true;	
}

//This is the JS function to validate whether the input within the specified length, 
//if the input is valid it will be passed to further process else the required input will be ask to the end user
function validFieldLength(fieldName,msg,start,end)
{
	if(trim(fieldName.value)!="")
	{		
		if(end!=null && start!=null)
		{
			if(fieldName.value.length>=start && fieldName.value.length<=end)
			{
				return true;
			}
			else
			{
				alert(msg);
				fieldName.select();
				return false;				
			}
		}
		else if(end!=null && start==null)
		{
			if(fieldName.value.length<=end)
			{
				return true;
			}
			else
			{
				alert(msg);
				fieldName.select();
				return false;				
			}
		}
		else
		{
			if(fieldName.value.length>=start)
			{
				return true;
			}
			else
			{
				alert(msg);
				fieldName.select();
				return false;				
			}
		}		
		
	}
	return true;	
}

//This is the JS function to validate whether the intger & float numbers input within the specified length, 
//if the input is valid it will be passed to further process else the required input will be ask to the end user
function validRange(fieldName,msg,start,end)
{

	if(trim(fieldName.value)!="")
	{	
		if(validInteger(fieldName,null)==true || validFloat(fieldName,null)==true)
		{
			if(end!=null && start!=null)
			{
				if(fieldName.value>=start && fieldName.value<=end)
				{
					return true;
				}
				else
				{
					alert(msg);
					fieldName.select();
					return false;				
				}
			}
			else if(end!=null && start==null)
			{
				if(fieldName.value<=end)
				{
					return true;
				}
				else
				{
					alert(msg);
					fieldName.select();
					return false;				
				}
			}
			else
			{
				if(fieldName.value>=start)
				{
					return true;
				}
				else
				{
					alert(msg);
					fieldName.select();
					return false;				
				}
			}	
		}
		else
		{				
			alert('Please enter valid integer or float number');
			fieldName.select();
			return false;								
		}
	}
	return true;	
}

//This is the JS function to compare the two fields input, 
//if the result is valid it will be passed to further process else the required input will be ask to the end user
function compareFields(fieldName1,fieldName2,cmp,msg)
{

	if(trim(fieldName1.value)!="" && trim(fieldName1.value)!="")
	{
			
		if(cmp=="=")
		{
			if(fieldName1.value==fieldName2.value)
			{				
				return true;
			}
			else
			{
				alert(msg);
				fieldName1.select();
				return false;				
			}
		}
		else if(cmp==">")
		{
			if(fieldName1.value>fieldName2.value)
			{
				
					return true;
			}
			else
			{
				alert(msg);
				fieldName1.select();
				return false;				
			}
		}
		else if(cmp=="<")
		{
			if(fieldName1.value<fieldName2.value)
			{
				
					return true;
			}
			else
			{
				alert(msg);
				fieldName1.select();
				return false;				
			}
		}
		else if(cmp=="<=")
		{
			if(fieldName1.value<=fieldName2.value)
			{
				
					return true;
			}
			else
			{
				alert(msg);
				fieldName1.select();
				return false;				
			}
		}
		else if(cmp==">=")
		{
			if(fieldName1.value>=fieldName2.value)
			{
				
					return true;
			}
			else
			{
				alert(msg);
				fieldName1.select();
				return false;				
			}
		}	
		
			
	}
	return true;	
}

//This is the JS function to validate whether the file is in the specified format, 
//if the input is valid it will be passed to further process else the required input will be ask to the end user
function validFileFormat(fieldName,format)
{	
	var valid = 0;
	var msg = "";
	var farray = format.split('|');
	//alert(farray);
	if(fieldName.value.length>0)
	{		
		fname = fieldName.value.split('.')
		var length = fname.length;		
		for(var i=0;i<farray.length;i++)
		{	
			if(farray.length>0)
			{	
				if(fname[length-1] == farray[i])
				{
					valid=valid+1;						
				}		
				else
				{
					if(i!=(farray.length-1))
					{
						msg = msg+farray[i]+", ";
					}
					else
					{
						msg = msg+farray[i];
					}	
				}		
				
			}
			else
			{
				msg=msg+farray[i];
			}
		}			
		if(valid==0)
		{
			msg = "Please attach "+msg+" format file only";
			alert(msg);
			fieldName.select();
			return false;
		}
		else
		{
			return true;
		}			
	}
	return true;	
}

//This is the JS function to validate whether the input is a floating point number, 
//if the input is valid it will be passed to further process else the required input will be ask to the end user
function validFloat(fieldName,msg)
{
	re= /^((\d+(\.\d*)?)|((\d*\.)?\d+))$/;
	if(trim(fieldName.value)!="")
	{	
		if(re.test(fieldName.value)==false)
		{
			if(msg!=null)
			{
				alert(msg);
			}
			fieldName.select();
			return false;
		}
	}
	return true;
}

//This is the JS function to validate whether the input is a valid integer number, 
//if the input is valid it will be passed to further process else the required input will be ask to the end user
function validInteger(fieldName,msg)
{
	re = /^\d+$/;
	if(trim(fieldName.value)!="")
	{	
		if(re.test(fieldName.value)==false)
		{
			if(msg!=null)
			{
				alert(msg);
			}
			fieldName.select();
			return false;
		}
	}
	return true;
}


//function for print
function printit(arg){  

	if (arg){
		document.getElementById(arg).style.display = "none";
	}
	
	 if (window.print) {
  	 	window.print();  
	} else {
  	 	var WebBrowser = '<OBJECT ID="WebBrowser1" WIDTH=0 HEIGHT=0 CLASSID="CLSID:8856F961-340A-11D0-A96B-00C04FD705A2"></OBJECT>';
		document.body.insertAdjacentHTML('beforeEnd', WebBrowser);
    	WebBrowser1.ExecWB(6, 2);//Use a 1 vs. a 2 for a prompting dialog box    WebBrowser1.outerHTML = "";  
   } 
    //opener.location='index.php?c=frontdesk&m=successpay'
	//window.self.close()
}
//function for close the current page
function closePage()
{
	window.self.close()
}

//this is for date comparion
function getDateObject(dateString,dateSeperator)
{
	//This function return a date object after accepting
	//a date string ans dateseparator as arguments
	var curValue=dateString;
	var sepChar=dateSeperator;
	var curPos=0;
	var cDate,cMonth,cYear;
	
	//extract day portion
	curPos=dateString.indexOf(sepChar);
	cDate=dateString.substring(0,curPos);
	
	//extract month portion
	endPos=dateString.indexOf(sepChar,curPos+1); cMonth=dateString.substring(curPos+1,endPos);
	
	//extract year portion
	curPos=endPos;
	endPos=curPos+5;
	cYear=curValue.substring(curPos+1,endPos);
	
	//Create Date Object
	dtObject=new Date(cYear,cMonth,cDate);
	return dtObject;
}
//code for change password
function changeMasterPass()
{
	
	 if(document.changepass.oldpass.value=='')
	{
		alert('Please enter the current password');
		document.changepass.oldpass.focus();
		return false;
		
	}
	 if(document.changepass.newpass.value=='')
	{
		alert('Please enter the new password');
		document.changepass.newpass.focus();
		return false;
		
	}
	else if(document.changepass.repass.value=='')
	{
		alert('Please re-type the new password');
		document.changepass.repass.focus();
		return false;
		
	}
	
	else if(document.changepass.newpass.value!=document.changepass.repass.value)
	{
		alert('The new password & confirm password are not the same, so please re-enter the confirm password');
		document.changepass.repass.focus();
		document.changepass.repass.value='';
		return false;
	}
	return true;
}

var xmlHttp
function get_frm(str,str1,str2)
{ 
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	{
	alert ("Browser does not support HTTP Request")
	return
	}
	var url=str
	url=url+"?id="+str1
	url=url+"&sid="+Math.random()
	xmlHttp.onreadystatechange=function(){
		
		/*if (xmlHttp.readyState!=4 || xmlHttp.readyState!="complete"){ 
			document.getElementById('hidepage').style.display='block';
		}*/
		 if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
		
				document.getElementById(str2).innerHTML=xmlHttp.responseText;
				//document.getElementById('hidepage').style.display='none';
			}			
		 };
	//document.getElementById('hidepage').style.display='none';	 
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
}

function GetXmlHttpObject()
{ 
	var objXMLHttp=null
	if (window.XMLHttpRequest)
	{
		objXMLHttp=new XMLHttpRequest()
	}
	else if (window.ActiveXObject)
	{
		objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
	}
	return objXMLHttp
}
// This is function for show Hide the dive according the Div ID
function showHideDiv(id,divId)
{
 
 if(id==1){
 	document.getElementById(divId).style.display='block';
 }else{
 	document.getElementById(divId).style.display='none';
 }

}
