/*	Javascript Document for validations	*/
/* ########################################################################
// Javascript functions for validating the information on pages
// Author		:	Gavin Fonseca.
// Date			:	15th May, 2007.
// Last Updated	:
######################################################################## */

// this function will shw the message 'msg' in the div 'divID'
function showerror(divID,msg,path)
{
	//alert(msg)
	/*if(divID!=""){
		ermsg = "<img src='http://crystal/jimspost/images/icon_error.gif' alt='Error Message' width='18' height='16' hspace='5' border='0'> "+msg;
		document.getElementById(divID).innerHTML = ermsg;
	}
	else{*/
		alert(msg);
	//}
}	// function showerror(divID,msg)

// this function will nullify contents of div 'divID'
function hideerror(divID)
{
	if(divID!="")
		document.getElementById(divID).innerHTML = "";	
}	// function hideerror(divID)

// this function will trim a string.
//str=> string to be trimmed.
//flag=> 'ltrim'-> trim left spaces | 'rtrim'-> trim right spaces | else trim all spaces
function trimStr(str,flag)
{
	if(flag=="ltrim")
		return str.replace(/^\s+/,"");
	else if(flag=="rtrim")
		return str.replace(/\s+$/,"");
	else
		return str.replace(/^\s+|\s+$/g,"");
	
}	// function hideerror(divID)


// checks whether length of a value is less than length specified.
//	maxlen => length to compare with
//	elmtId => id of the elemt to be evaluated
//	divID => id of the div in which message will be displayed
// 	msg => message to be displayed
function chkStrLen(maxlen,elmtId,divID,msg,path)
{
	var str = document.getElementById(elmtId).value;
	if (str.length > maxlen){
		showerror(divID,msg,path);
		document.getElementById(elmtId).focus();
		return false;
	}
	else{
		hideerror(divID);
		return true;
	}
}	// function chkStrLen()


// checks whether value of an element is null.
//	elmtId => id of the elemt to be evaluated
//	divID => id of the div in which message will be displayed
// 	msg => message to be displayed
function chkLength(elmtId,len,divID,msg,path)
{	
	var strlen = document.getElementById(elmtId).value.length
	if (strlen > len)
	{
		showerror(divID,msg+" can contain a maximun of 500 characters.\nYou have currently entered "+strlen+" characters",path);
		document.getElementById(elmtId).focus();
		return false;
	}
	else{
		hideerror(divID);
		return true;
	}
}	// function chkLength(elmt,msg)



// checks whether value of an element is null.
//	elmtId => id of the elemt to be evaluated
//	divID => id of the div in which message will be displayed
// 	msg => message to be displayed
function chkNull(elmtId,divID,msg,path)
{	
	var str = trimStr(document.getElementById(elmtId).value,"ltrim");
	str = trimStr(str,"rtrim");
	
	if (str=="")
	{
		showerror(divID,msg,path);
		document.getElementById(elmtId).value="";
		document.getElementById(elmtId).focus();
		return false;
	}
	else{
		hideerror(divID);
		return true;
	}
}	// function chkNull(elmt,msg)

//  checks whether email id entered is valid.
//	elmtId => id of the elemt to be evaluated
//	divID => id of the div in which message will be displayed
// 	msg => message to be displayed
function chkEmail(elmtId,divID,msg,path)
{
   	var re = /^[\w-]+(\.[\w-]+)*@([\w-]+\.)+[a-zA-Z]{2,7}$/;
    //var re = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
    if (!document.getElementById(elmtId).value.match(re))
	{
		showerror(divID,msg,path);
		document.getElementById(elmtId).focus();
        return false;
    }
	else{ hideerror(divID); return true; }
}	// function chkEmail(val)
/*
function isEmail(string)
{
	if (string.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1)
	return true;
	else
	return false;
}
*/

//  checks whether password entered is valid.
//	elmtId => id of the elemt to be evaluated
//	divID => id of the div in which message will be displayed
// 	msg => message to be displayed
function chkPass(elmtId,divID,msg,path)
{
	//var re = /^\s+/;
	var invalid = " ";
	var str = document.getElementById(elmtId).value;
    //var re = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
    if (str.indexOf(invalid) > -1 || str=="")
	{
		showerror(divID,msg,path);
		document.getElementById(elmtId).focus();
        return false;
    }
	else{ hideerror(divID); return true; }
}	// function chkPass(val)


//  checks whether email id entered is valid.
//	elmtId => id of the elemt to be evaluated
//	divID => id of the div in which message will be displayed
// 	msg => message to be displayed
function chkName(elmtId,divID,msg,path)
{
	var str = trimStr(document.getElementById(elmtId).value,"ltrim");
	str = trimStr(str,"rtrim");
	
    var re = "/^([a-zA-Z\'\ ]+)$/";
    if (!re.test(str) || str=="")
	{
		showerror(divID,msg,path);
		document.getElementById(elmtId).focus();
        return false;
    }
	else{ hideerror(divID); return true; }
}	// function chkEmail(val)

//  checks whether username is valid.
//	elmtId => id of the elemt to be evaluated
//	divID => id of the div in which message will be displayed
// 	msg => message to be displayed
function chkUsername(elmtId,divID,msg,path)
{
	var val = document.getElementById(elmtId).value;
	
	// allow only letters, numbers, and underscores. should start with alphabet & end with alp or num.
	if (val.search(/^[A-Za-z ]+(\w+)([A-Za-z]|[0-9]+)$/) == -1){
		showerror(divID,msg,path);
		document.getElementById(elmtId).focus();
        return false;
    }
	else{ hideerror(divID); return true; }
}	// function chkEmail(val)

//  checks whether date is valid.
//	d => id of the date field	/	m => id fo the month field	/	y => id of the year field
//	elmtId => id of the elemt to be evaluated
//	divID => id of the div in which message will be displayed
// 	msg => message to be displayed
function chkDob(d,m,y,divID,msg,path)
{
	if (d.value=="" || m.value=="" || y.value=="" || isNaN(d.value) || isNaN(m.value) || isNaN(y.value))
	{
		showerror(divID,msg,path);
		d.focus();
		return false;
	}
	else{ hideerror(divID); }
	
	if(m.value>12 || m.value < 1)
	{	
		showerror(divID,msg,path);
		m.focus();
		return false;
	}
	
	if (m.value==4 || m.value==6 || m.value==9 || m.value==11)
	{
		if (d.value>30)
		{
			showerror(divID,msg,path);
			d.focus();
			return false;
		}
		else{ hideerror(divID); }
	}
	else{
		if (d.value>31)
		{
			showerror(divID,msg,path);
			d.focus();
			return false;
		}
		else{ hideerror(divID); }
	}
	
	var year = y.value;
	if(year.length==2){
		year = "20"+year;
	}
	
	if ( (year%4)==0 )
	{
		if (m.value==2)
		{
			if (d.value>29)
			{
				showerror(divID,msg,path);
				d.focus();
				return false;
			}else{ hideerror(divID); }
		}else{ hideerror(divID); }
	}
	else
	{
		if (m.value==2)
		{
			if (d.value>28)
			{
				showerror(divID,msg,path);
				d.focus();
				return false;
			}
			else{ hideerror(divID); return true; }
		}else{ hideerror(divID); return true; }
	}
}	// function chkDob(elmt,msg)

function chkDate(mthId,yrId,ylen,maxyr,divID,msg,path)
{
	var m = document.getElementById(mthId).value;
	var y = document.getElementById(yrId).value;
	var dt = new Date;
	maxYr = dt.getFullYear()+maxyr;
	
	if (m=="" || y==""){
		showerror(divID,"Please enter a proper "+msg,path);
		document.getElementById(mthId).focus();
		return false;
	}	
	else if (m > 12 || m < 1){
		showerror(divID,"Please enter a proper month value for "+msg,path);
		document.getElementById(mthId).focus();
		return false;
	}
	else if (y.length != ylen || y > maxYr || y < dt.getFullYear()){
		showerror(divID,"Please enter a proper year value for "+msg,path);
		document.getElementById(yrId).focus();
		return false;
	}
	else{ hideerror(divID); }
}	//function chkDate(mthId,yrId,divId,msg,path)

//  checks whether value is a number
//	elmtId => id of the elemt to be evaluated
//	divID => id of the div in which message will be displayed
// 	msg => message to be displayed
function chkNum(elmtId,divID,msg,path)
{	
	var str = trimStr(document.getElementById(elmtId).value,"ltrim");
	str = trimStr(str,"rtrim");
	
	if (str=="" || isNaN(document.getElementById(elmtId).value))
	{
		showerror(divID,msg,path);
		document.getElementById(elmtId).value="";
		document.getElementById(elmtId).focus();
		return false;
	}
	else{
		hideerror(divID);
		return true;
	}
}	// function chkNum(elmt,msg)

//  CUSTOM FUNCTION *** checks for price values
//	elmtId => id of the elemt to be evaluated
//	divID => id of the div in which message will be displayed
// 	msg => message to be displayed
function chkPrice(elmtId,divID,msg,path)
{
	var price = document.getElementById(elmtId).value;
	var arrPrice = price.split(".",2);
	//alert(arrPrice[1]);
	if (arrPrice[1]!="99")
	{
		showerror(divID,msg,path);
		document.getElementById(elmtId).focus();
		return false;
	}
	else{
		hideerror(divID);
		return true;
	}
}	// function chkNum(elmt,msg)

//	checks whether checkbox is selected.
//	elmtId => id of the elemt to be evaluated
//	divID => id of the div in which message will be displayed
// 	msg => message to be displayed
function chkCheckbox(elmt,cnt,divID,msg,path)
{
	var isChk = "";
	document.forms[0].hidChecks.value = "";
	for (i=0; i<cnt; i++)
	{
		if (elmt[i].checked==true)
		{
			document.forms[0].hidChecks.value += elmt[i].value+",";
			isChk = "true";
		}
	}
	if (isChk!="true")
	{
		showerror(divID,msg,path);
		return false;
	}
	else{
		hideerror(divID);
		return true;
	}
}	// function chkCheckbox(elmt,cnt,divID,msg)

// Author : Gavin Fonseca.
// Purpose : Validate the time.
// time - Time, format - 'h:m' OR 'h:m:s' OR 'hh:mm' OR 'hh:mm:ss'(default)
//clock - '12hr' OR '24hr' (default)
// Note: hh:mm:ss & hh:mm formats are strictly 2 digits. 
function chkTime(time, format, clock, divId, msg, path)
{
	var error;
	if(time==""){
		error=1;
	}
	else{
		var splitnum;
		if (format=="h:m" || format=="hh:mm"){
			splitnum=2;
			arrlen=2;
		}
		else if (format=="h:m:s" || format=="hh:mm:ss" || format==""){
			splitnum=3;
			arrlen=3;
		}			
		arrtime = time.split(":",splitnum);
		if (arrtime.length!=arrlen){
			error=1;
		}
		
		if (arrtime[0]=="" || arrtime[0]<=0 || arrtime[1]=="" || arrtime[1]<0){
			error=1;
		}
		else{
			if (clock=="24hr" || clock==""){
				if(arrtime[0]>23){
					error=1;
				}
			}
			else if(clock=="12hr"){
				if(arrtime[0]>12 || arrtime[0]=="00"){
					error=1;
				}
			}
			
			if (arrtime[1]>59){
				error=1;
			}
			
			if (splitnum==3){
				if (arrtime[2]>59 || arrtime[2]<0){
					error=1;
				}
			}	// if (splitnum==3)
			
			if (format=="hh:mm"){
				if (arrtime[0].length < 2 || arrtime[1].length < 2){
					error=1;
				}
			}
			else if (format=="hh:mm:ss"){
				if (arrtime[0].length < 2 || arrtime[1].length < 2 || arrtime[2].length < 2){
					error=1;
				}
			}	// else if (format=="HH:ii")
			
		}	// else of if (arrtime[0]=="" || arrtime[0]<=0 || arrtime[1]=="" || arrtime[1]<0)
	}	// else of if(time=="")
	
	if (divId!="" && error>0){
		showerror(divId,msg,path);
		return false;
	}
}

function getcurDate(form)
{
	var d = new Date();
	var day = d.getDate();
	var month = d.getMonth();
	var year = d.getFullYear();
	
	if(form=="dmy")
		return  day+"-"+month+"-"+year;
	if(form=="mdy")
		return  month+"-"+day+"-"+year;
	if(form=="ymd")
		return  year+"-"+month+"-"+day;
}

function chkIP (IPvalue,divId,path)
{
	errorString = "";
	theName = "IP Address";
	
	var ipPattern = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/;
	var ipArray = IPvalue.match(ipPattern);
	
	if (IPvalue == "0.0.0.0")
		errorString = errorString + theName + ': '+IPvalue+' is a special IP address and cannot be used here.';
	else if (IPvalue == "255.255.255.255")
		errorString = errorString + theName + ': '+IPvalue+' is a special IP address and cannot be used here.';
	if (ipArray == null)
		errorString = errorString + theName + ': '+IPvalue+' is invalid.';
	else {
		for (i = 0; i < 4; i++) {
			thisSegment = ipArray[i];
			if (thisSegment > 255) {
				errorString = errorString + theName + ': '+IPvalue+' is not a valid IP address.';
				i = 4;
			}
			if ((i == 0) && (thisSegment > 255)) {
				errorString = errorString + theName + ': '+IPvalue+' is a special IP address and cannot be used here.';
				i = 4;
			}
		}
	}
	extensionLength = 3;
	if (errorString != ""){
		showerror(divId,errorString,path);
		return false;
	}
	else{ hideerror(divId); return true; }
}
