// 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 compareValiddate(value1,value2)/// To compare two dates
{
   var firstIndex1 = value1.indexOf ("/");
   var lastIndex1 = value1.lastIndexOf ("/");
   month1 = value1.substring (0, firstIndex1);
   date1 = value1.substring (firstIndex1+1, lastIndex1);
   year1 = value1.substring (lastIndex1+1, value1.length);

   var firstIndex2 = value2.indexOf ("-");
   var lastIndex2 = value2.lastIndexOf ("-");
   month2 = value2.substring (0, firstIndex2);
   date2 = value2.substring (firstIndex2+1, lastIndex2);
   year2 = value1.substring (lastIndex2+1, value2.length);
   
   var mymsg = 0;
   
   if(year1 >= year2)
   {
	   if(month1 >= month2)
	   {
		  if(month1 > month2)
			mymsg = 1;
		  else if(month1 == month2)
		  {
			   if(date1 >= date2)
				 mymsg = 1;
			   else
			     mymsg = 0;
		   }
	   }
	   else
	     mymsg = 0;
   }
   else
     mymsg = 0
  return mymsg;
}


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;
}

function checkfax(e)// validation to enter only numbers
{
  if(window.event)
  {
		if((e.keyCode > 1 && e.keyCode != 43 && e.keyCode != 45 && e.keyCode < 47 && e.keyCode!=8) ||(e.keyCode > 58 && e.keyCode < 255) ) 
		return false;
  }
  else if(e.which)
   {
		if((e.which > 1 && e.keyCode != 43 && e.keyCode != 45 && e.which < 47 && e.keyCode!=8) ||(e.which > 58 && e.which < 255) ) 
		return false;
   }
   return true;
}
/****** For date validation *********/
function isValidDate(element,msg)
{
 	var date1=element.value;
	//var date2=frm.eDate.value;
	var now    = new Date();
	var monthnumber = now.getMonth();
	var month       = (monthnumber)+1;
	var monthday    = now.getDate();
	var year        = now.getFullYear();
	function padZero(num) {
		return (num	< 10)? '0' + num : num ;
	}
	
    var tdate       = padZero(month) + '-' + padZero(monthday) + '-' + year;
	var mymsg
		mymsg = '';
     if (date1 != '') {
				var stDate = compareValiddate(date1,tdate);
				if (stDate == 0) {
					mymsg = mymsg + msg;
				}
			}
			/*if (frm.eDate.value != '') {
				var endDate = compareDates(frm.eDate.value,'M-d-y',tdate,'M-d-y');
				if (endDate == 0) {
					mymsg = mymsg + 'end  Date must be greater than today.';
				}
			}*/
			// 1 if date1 is greater than date2
			// 0 if date2 is greater than date1 of if they are the same
			// -1 if date invalid check your formats
			
			/*var delivCompare = compareDates(frm.sDate.value,'M-d-y',frm.eDate.value,'M-d-y');
			if (delivCompare == 1) {
					mymsg = mymsg + '\nYou have entered a end Date that is less than the start  Date.'
			}*/
			
			
			if (mymsg != '') {
				alert(mymsg);
				return false;
			}
	       return true;
	
}//function

/*
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 to validate ading venue page
//Page : addvenue.php
function fnValidate_AddVenue(frm)
{
	//chk the location type
/*	if(!isValidSelect1(frm.elements['add[ven_type]'],"location type"))
		return false;
*/		
	//chk whether venue	name is entered or not
	if(!isValidEntry(frm.elements['add[ven_name]'],"venue name"))
		return false;
/*		
	//chk whether unit no. is entered or not
	if(!isValidEntry(frm.elements['add[ven_unit_no]'],"unit no."))
		return false;
	
	//chk whether street no. is entered or not
	if(!isValidEntry(frm.elements['add[ven_street_no]'],"street no."))
		return false;
		
	//chk whether street name is entered or not
	if(!isValidEntry(frm.elements['add[ven_street]'],"name of street"))
		return false;
		
	//chk whether city is selected or not
	if(!isValidSelect1(frm.elements['add[ven_city]'],"city"))
		return false;
	
*/
	//chk whether suburb name is entered or not
	if(!isValidEntry(frm.elements['add[ven_suburb]'],"name of suburb"))
		return false;
	
	//chk whether post code is entered or not
	if(!isValidEntry(frm.elements['add[ven_pcode]'],"post code"))
		return false;
		
	//chk whether the length of post code is 4 or not
	if(frm.elements['add[ven_pcode]'].value.length > 4)
	{
		alert("Post code should contain a maximum of 4 digits");
		frm.elements['add[ven_pcode]'].focus();
		return false;
	}
/*	
	//chk whether venue tags is given or not
	if(!isValidEntry(frm.elements['add[ven_tags]'],"venue tags"))
		return false;
	
	//chk whether venue desription is given or not
	if(!isValidEntry(frm.elements['add[ven_desc]'],"venue description"))
		return false;
	
	//chk whether parking n transport info is entered or not
	if(!isValidEntry(frm.elements['add[ven_transport_park_info]'],"parking information"))
		return false;
	
	
	//chk whether any ic is uploaded for venue
	if(frm.elements['ven_photo'].value == "")
	{
		alert("Please upload the venue photo");	
		frm.ven_photo.focus();
		return false;
	}
*/	
	
	frm.ven_act.value = "add";
	
	//redirect to previous page
	/*if(frm.hdBackTo.value == "AddEvent")
	{
		frm.action = "add_event/new_event.php";	
	}
	else if(frm.hdBackTo.value == "EditLoc")
	{
		frm.action = "edit_mymix/location.php";
	}*/
	
	
}

function Header_FooterSearch(frm)
{
//	var frm = document.HeaderNFooterSrch;
//alert(frm.keyword);
	if(frm.keyword.value == " Name, venue, suburb, dd/mm/yy" || frm.keyword.value == "")
	{
		alert("Please Specify Search Keyword");
		frm.keyword.focus();
		return;
	}
	
	frm.exp_act.value = "Search";
	frm.submit();
}


