/*#####################################################*/
//	Summary:			This is the Functions file.
//	File Creator Name:	Amit Kumar
//	Date of creation:	July 04, 2005
//	Contact information: 
//	Email:			akumar@hanusoftware.com
//		Phone:			09818380723
//	Last Updated by:	Amit Kumar
//	Last Updated Date:	July 04, 2005
//	Contact information:
//		Email:			akumar@hanusoftware.com
//		Phone:			09818380723
/*#####################################################*/

//Function to Check the Email
function emailCheck(x)
{
	x = x.replace(/^\s*|\s*$/g,"");
    if(x=='') return true;
	var filter  = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (filter.test(x)) return true;
	else alert('Email address is incorrect.');
	return false;
}

//Funcion to Show the Alert when user try to Delete Something
function confirmDelete()
{
var agree=confirm("Are you sure you wish to continue?");

//alert (agree);

if (agree)
	return true ;
else
	return false ;
}

//Function to Check or Uncheck All Select Box
function checkUncheckAll(theElement) 
{	
     var theForm = theElement.form, z = 0;
     while (theForm[z].type == 'checkbox') {
      theForm[z].checked = theElement.checked;
      z++;
     }
}


// get element value after removing leading and trailing spaces
function RemoveLTSpace(elemval)
{
	var val=elemval.replace(/\s*/,"")
	var val=val.replace(/\s*$/,"")
	return val;
}


// This function is to check the float values OnKeyPress
function numbersonly(str)
{
	var key;
	var keychar;
	var e=window.onkeypress;

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

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

	// numbers
	else if (((str).indexOf(keychar) > -1))
	   return true;

	// decimal point jump
/*	else if (dec && (keychar == "."))
	   {
	alert(dec);
	   myfield.form.elements[dec].focus();
	   return false;
	   }
*/
	else
	{
	   return false;
	}
}


//Function to Copy Data from source List to Destination List
function copyToList(from,to)
{
  fromList = eval('document.forms[0].' + from);
  toList = eval('document.forms[0].' + to);
  if (toList.options.length > 0 && toList.options[0].value == "")
  {
    toList.options.length = 0;
  }
  var sel = false;
  for (i=0;i<fromList.options.length;i++)
  {
    var current = fromList.options[i];
    if (current.selected)
    {
      sel = true;
      if (current.value == "")
      {
        alert ('You cannot move this text!');
        return;
      }
      txt = current.text;
      val = current.value;
	  alreadyexist=false;
	  for(j=0;j<toList.options.length;j++)
	  {
		if(toList.options[j].text==txt) alreadyexist=true;
	  }
	  if(!alreadyexist)
      toList.options[toList.length] = new Option(txt,val);
//      fromList.options[i] = null;
//      i--;
    }
  }
  if (!sel) alert ('You haven\'t selected any options!');
}


//Function to Remove Data from Destination List
function removeFromList(from)
{
  fromList = eval('document.forms[0].' + from);
  for (i=0;i<fromList.options.length;i++)
  {
    if(fromList.options[i].selected)
	{
      fromList.options[i] = null;
      i--;
	}
  }
}



//Function to Check the Date

function check_date(field)
{
var checkstr = "0123456789";
var DateField = field;
var Datevalue = "";
var DateTemp = "";
var seperator = ".";
var day;
var month;
var year;
var leap = 0;
var err = 0;
var i;
   err = 0;
   DateValue = DateField.value;
   /* Delete all chars except 0..9 */
   for (i = 0; i < DateValue.length; i++) {
	  if (checkstr.indexOf(DateValue.substr(i,1)) >= 0) {
	     DateTemp = DateTemp + DateValue.substr(i,1);
	  }
   }
   DateValue = DateTemp;
   /* Always change date to 8 digits - string*/
   /* if year is entered as 2-digit / always assume 20xx */
   if (DateValue.length == 6) {
      DateValue = DateValue.substr(0,4) + '20' + DateValue.substr(4,2); }
   if (DateValue.length != 8) {
      err = 19;}
   /* year is wrong if year = 0000 */
   year = DateValue.substr(4,4);
   if (year == 0) {
      err = 20;
   }
   /* Validation of month*/
   month = DateValue.substr(2,2);
   if ((month < 1) || (month > 12)) {
      err = 21;
   }
   /* Validation of day*/
   day = DateValue.substr(0,2);
   if (day < 1) {
     err = 22;
   }
   /* Validation leap-year / february / day */
   if ((year % 4 == 0) || (year % 100 == 0) || (year % 400 == 0)) {
      leap = 1;
   }
   if ((month == 2) && (leap == 1) && (day > 29)) {
      err = 23;
   }
   if ((month == 2) && (leap != 1) && (day > 28)) {
      err = 24;
   }
   /* Validation of other months */
   if ((day > 31) && ((month == "01") || (month == "03") || (month == "05") || (month == "07") || (month == "08") || (month == "10") || (month == "12"))) {
      err = 25;
   }
   if ((day > 30) && ((month == "04") || (month == "06") || (month == "09") || (month == "11"))) {
      err = 26;
   }
   /* if 00 ist entered, no error, deleting the entry */
   if ((day == 0) && (month == 0) && (year == 00)) {
      err = 0; day = ""; month = ""; year = ""; seperator = "";
   }

   /* if no error, write the completed date to Input-Field (e.g. 13.12.2001) */
   if (err == 0) {
      DateField.value = day + seperator + month + seperator + year;
	  return true;
   }
   /* Error-message if err != 0 */
   else {
      alert("Date is incorrect!");
	  return false;
      //DateField.select();
	  //DateField.focus();
   }
}

//Function to refresh page and get id when select box changes
function onchangeselect(id,page)
{
	top.location=page+"?id="+id;
}


//Function to refresh page and pass two values in url
function onchangetwo(id,id1,page)
{
	top.location=page+"?id="+id+"&id1="+id1;
}

//function to check the url entered is Correct or not
function chkurl(urlentered)
{
	var url = urlentered;
	var urlRegxp = /^(http:\/\/www.|https:\/\/www.|ftp:\/\/www.|www.){1}([\w]+)(.[\w]+){1,2}$/;
	if (urlRegxp.test(url) != true)
	{
	alert("URL appears to be incorrect!");
	return false;	
	}
	else
	{
		return true;
	}
}
/*
function chkurl(address) 
{
  if ((address == "")  || (address.indexOf ('http://') == -1) || (address.indexOf ('www') == -1)  || (address.indexOf ('.') == -1))
	{
      return false;
	}
	else
	{
		return true;
	}
}*/

//Function for Window PopUp
function window_popup(url,name,h,w)
{
	newwindow=window.open(url,name,'height='+h+',width='+w+',scrollbars,resizable=yes');
	if (window.focus) {newwindow.focus()}
	return false;
}


//Function for popup
function popitup(url)
{
	newwindow=window.open(url,'name','height=350,width=500,scrollbars');
	if (window.focus) {newwindow.focus()}
	return false;
}

//Function for popup
function popituphw(url,h,w)
{
	newwindow=window.open(url,'name','height='+h+',width='+w+',scrollbars');
	if (window.focus) {newwindow.focus()}
	return false;
}


//Function to Get the querystring from URL
function PageQuery(q) 
{
	if(q.length > 1) this.q = q.substring(1, q.length);
	else this.q = null;
	this.keyValuePairs = new Array();
	if(q) 
	{
		for(var i=0; i < this.q.split("&").length; i++) 
		{
			this.keyValuePairs[i] = this.q.split("&")[i];
		}
	}
	this.getKeyValuePairs = function() { return this.keyValuePairs; }
	this.getValue = function(s) {
		for(var j=0; j < this.keyValuePairs.length; j++) {
			if(this.keyValuePairs[j].split("=")[0] == s)
				return this.keyValuePairs[j].split("=")[1];
		}
		return false;
	}

	this.getParameters = function() {
		var a = new Array(this.getLength());
		for(var j=0; j < this.keyValuePairs.length; j++) {
			a[j] = this.keyValuePairs[j].split("=")[0];
		}
		return a;
	}

	this.getLength = function() { return this.keyValuePairs.length; }	
}

//Function to Get the querystring from URL calling pagequery

function queryString(key)
{
	var page = new PageQuery(window.location.search); 
	return unescape(page.getValue(key)); 
}



//Function to Check that Date is Correct or Not
function isValidDate(dateStr) {
// Checks for the following valid date formats:
// MM/DD/YY   MM/DD/YYYY   MM-DD-YY   MM-DD-YYYY
// Also separates date into month, day, and year variables

var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{2}|\d{4})$/;

// To require a 4 digit year entry, use this line instead:
// var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/;

var matchArray = dateStr.match(datePat); // is the format ok?
if (matchArray == null) {
alert("Date is not in a valid format.")
return false;
}
month = matchArray[1]; // parse date into variables
day = matchArray[3];
year = matchArray[4];
if (month < 1 || month > 12) { // check month range
alert("Month must be between 1 and 12.");
return false;
}
if (day < 1 || day > 31) {
alert("Day must be between 1 and 31.");
return false;
}
if ((month==4 || month==6 || month==9 || month==11) && day==31) {
alert("Month "+month+" doesn't have 31 days!")
return false
}
if (month == 2) { // check for february 29th
var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
if (day>29 || (day==29 && !isleap)) {
alert("February " + year + " doesn't have " + day + " days!");
return false;
   }
}
return true;  // date is valid
}

//Function to check integer
function isInt (str)
{
	var i = parseInt (str);

	if (isNaN (i))
		return false;

	i = i . toString ();
	if (i != str)
		return false;

	return true;
}

//checks the format of the phone no.
function chkphone(str)
{
	var pattern3 = /\d{3}\-\d{3}\-\d{4}/;
	if(pattern3.test(str))
	{
		return true;
	}
	else
	{				
			return false;			
	}
	
}