// JavaScript Document

//Written by   : Ashwini S
//Description  : Common js functions
//Dated        : 13th June,07    


//function to chk whether given given text field is empty or not
function isValidEntry(element,msg) 
{
 	if(element.value.length == 0)
	{
		alert("Please enter the "+ msg);
		element.focus();
		return false;
	}
	return true;
} // closing the function isValidEntry()



//function to chk for valid URL
function isValidURL(element, msg, required)
{
	if(element.value == "")
	{
		var rval = trim(required);
		if (rval.toLowerCase() == "yes" || rval == 1)
		{
			alert("Please enter "+msg);
			element.focus();
			return false;
		}
	}
	if(element.value != "")
	{
		// if (!(/^[www]\w+([\.-]?\w+)*(\.\w{2,3}.*/i.test(element.value)))))
		var oRegExp = /[^:]+:\/\/[^:\/]+(:[0-9]+)?\/?.*/;
		if (!oRegExp.test(element.value))
		{
			alert('\r\n The URL you have entered is invalid.\n Please check it for accuracy.');
			element.focus();
			element.select();
			return false;
		}
	}
	return true;
}



//function to chk for valid email
function isValidEmail(VarEmail)
{
		if(VarEmail.value == "" || VarEmail.length == 0)
		{
				alert("Please enter Email Address");
				VarEmail.focus();
				return false;
		}	
		if(VarEmail.value!="")
        {
                if (!(/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(VarEmail.value)))
                {
				       alert("Invalid Email address!")
                        VarEmail.focus();
                        return false;
                }
        } 
		return true;
}


function check_alpha1(e)/// validation to enter only numbers
{
  if(window.event)
  {
		if((e.keyCode > 1 && e.keyCode < 47 && e.keyCode!=8) ||(e.keyCode > 58 && e.keyCode < 255) ) 
		return false;
  }
  else if(e.which)
   {
		if((e.which > 1 && e.which < 47 && e.keyCode!=8) ||(e.which > 58 && e.which < 255) ) 
		return false;
   }
   return true;
}


function checknum(e)// validation to enter only alphabets
{
	if(window.event)
	{
		if((e.keyCode>32 && e.keyCode<=64) ||  (e.keyCode>=91 && e.keyCode<=95) 
		|| (e.keyCode==96) || (e.keyCode>=123 && e.keyCode<=127)) 
		return false;
	}//if
	else if(e.which)
	{
		if((e.which>32 && e.which<=64) ||  (e.which>=91 && e.which<=95) 
		|| (e.which==96) || (e.which>=123 && e.which<=127)) 
		return false;
	}//else if
	return true;
}

//function to validate phone number
function fnCheck_Phone(e)
{
  if(window.event)
  {
		if((e.keyCode > 1 && e.keyCode != 45 && e.keyCode < 47 && e.keyCode!=8 && e.keyCode!=55) ||(e.keyCode > 58 && e.keyCode < 255) ) 
		return false;
  }
  else if(e.which)
   {
		if((e.which > 1 && e.which != 45 && e.which < 47 && e.which!=8 && e.which!=55) ||(e.which > 58 && e.which < 255) ) 
		return false;
   }
   return true;
}


/*
3. SELECT VALIDATION

Usage:
Element  name of the control, like frm.firstname
Message  Field Name that we want to display in alert message.

if(!isValidSelect(frm.country,'Country'))
return;
*/
function isValidSelect(element,msg) 
{
	if(element.value == "-1" || element.value == "" || element.value == 0) 
	{
		alert("Please select "+msg+" from the list");
		element.focus();
		return false;
	}
	return true;
}


function isValidSelect1(element,msg) 
{
	if(element == "-1" || element == "" || element == 0) 
	{
		alert("Please select "+msg+" from the list");
		element.focus();
		return false;
	}
	return true;
}

/*

4. PHONE NUMBER VALIDATION
Usage: 
Element  name of the control, like frm.phone
Message  Field Name that we want to display in alert message.
Required  Set this to yes if the field is mandatory, otherwise no.

 if(!isValidPhone(frm.phone,'Phone Number','yes'))
 return;
*/
function isValidPhone(element, msg, required)
{	
	var VarPhone = element.value;
	if (VarPhone== "")
	{	
		var rval = trim(required);
		if (rval.toLowerCase() == "yes" || rval == 1)
		{
			alert("Please enter "+msg);
			element.focus();
			return false;
		}
	}
	if (VarPhone != "")
	{
		var Phno;
		Phno=VarPhone;
		var valid = "-0123456789()";
		var hyphencount = 0;
		for (var i=0; i < Phno.length; i++) 
		{
			temp = "" + Phno.substring(i, i+1);
			if (valid.indexOf(temp) == "-1")
			{
				alert("Invalid characters in your "+msg+". Please try again.");
				element.focus();
				return false;
			}
		}
     } 
	 return true;      
}


//function to show the hand cursor for the image n buttons
//Page : ho_registration.php
function fnShowHand(frm,ctrl_id)
{
	var Cursor='hand';

	if (!document.all){ Cursor='pointer'; }
	
	//for mouseover
	
	document.getElementById(ctrl_id).style.cursor=Cursor;	
}


//function to show the hand cursor for the image
//Page : ho_registration.php
function fnRemoveHand(frm,ctrl_id)
{
	var Cursor='hand';

	if (!document.all){ Cursor='pointer'; }
	
	//for mouseout
	
	document.getElementById(ctrl_id).style.cursor='default';	
}

// function to trim leading & trailing spaces
function trim(strText) { 
    // this will get rid of leading spaces 
    while (strText.substring(0,1) == ' ') 
        strText = strText.substring(1, strText.length);

    // this will get rid of trailing spaces 
    while (strText.substring(strText.length-1,strText.length) == ' ')
        strText = strText.substring(0, strText.length-1);

   return strText;
} 

/*
2. NUMBER VALIDATION
Usage:
Element  name of the control, like frm.number
Message  Field Name that we want to display in alert message.
Required  Set this to yes if the field is mandatory, otherwise no.

 if(!isValidNumber(frm.num,'Roll Number','yes'))
 return;
*/
function isValidNumber(element, msg, required)
{  
	var VarNumber = element.value;
	if(VarNumber == "")
	{
		var rval = trim(required);
		if (rval.toLowerCase() == "yes" || rval == 1)
		{
			alert("Please enter "+msg);
			element.focus();
			return false;
		}
	}
	if (VarNumber != "")
	{
		var Num;
		Num=VarNumber;
		var valid = "0123456789";
		var hyphencount = 0;
		
		for (var i=0; i < Num.length; i++) 
		{
			temp = "" + Num.substring(i, i+1);
			if (valid.indexOf(temp) == "-1")
			{
			  alert("Invalid characters in your "+msg+".  Please try again.");
			  element.focus();
			  return false;
			}
	   } // end for loop
	   
		if(VarNumber < 1)
		{
			alert(msg+" is not a valid number");
			element.focus();
			return false;
		}
    }   // end if
    return true; 
}  // end function

function SortResults(frm,val)
{
	frm.sortby.value=val;
	frm.submit();
}


function fnShowPerPage(frm,val)
{
	frm.limit.value=val;
	frm.submit();
}

function fnShowPage(frm,pg,lmt)
{
	frm.pgcnt.value = pg;
	frm.limit.value = lmt;
	frm.submit();
}

function Sort_SearchResults(frm,val)
{
	frm.order_by.value=val;
	frm.submit();
}


function fnShowSearchPerPage(frm,val)
{
	frm.limit.value=val;
	frm.submit();
}

function fnShowSearchPage(frm,pg,lmt)
{
	frm.page.value = pg;
	frm.limit.value = lmt;
	frm.submit();
}


function fnSearchGo(frm)
{
//alert(frm.name);
	if(frm.q.value == "" || frm.q.value == 'Enter Keywords')
	{
		alert("Please Specify Search Keyword, then click GO!");
		frm.q.focus();
		return false;
	}
	return true;
	//frm.submit();
}

//function to check/uncheck all Phases
function fn_ChkAll(frm,val,ary_name)
{
	;
	var len = frm.elements[ary_name+'[]'].length;
	if(len == 1)
	{
		if(frm.elements[val].checked)
			frm.elements[ary_name].checked = true;
		else
			frm.elements[ary_name].checked = false;
	}
	for(i=0;i<len;i++)
	{
		if(frm.elements[val].checked)
			frm.elements[ary_name+'[]'][i].checked = true;
		else
			frm.elements[ary_name+'[]'][i].checked = false;
	}		   
}

function fn_uncheck(frm,val,ary_name)
{
	var len = frm.elements[ary_name+'[]'].length;
	var count =0;
	if(len == 1)
	{
		if(frm.elements[ary_name].checked)
			frm.elements[val].checked = true;
		else
			frm.elements[val].checked = false;
	}
	else
	{
		for(i=0;i<len;i++)
		{
			if(frm.elements[ary_name+'[]'][i].checked)
				count++;
		}
		if(count == len)
			frm.elements[val].checked = true;
		else
			frm.elements[val].checked = false;
	}//if else
}
