// JavaScript Document
// Various functions used in leads

var ie5 = (document.getElementById && document.all);
var ns6 = (document.getElementById && !document.all);

//image functions generated by DreamWeaver 
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function checkEmail(strng)
{
	
	var error = "";
	var emailFilter=/^.+@.+\..{2,4}$/;
	if (strng == "") 
		{
		error = "You didn't enter an email-address.\n";
		}
	
	else if (!(emailFilter.test(strng))) 
		{ 
		error = "Please enter a valid email address.\n";
		}
	var illegalChars= /[\(\)\<\>\,\;\:\\\/\"\[\]]/;
	if (strng.match(illegalChars))
		{
		error = "The email address contains illegal characters.\n";
		}
	return error;
}

function navigatePage(pageNo,numOfRec) {
	var frm = document.frmMain;	
	frm.currentPage.value = pageNo;
	
	frm.submit();
}

function goToOperation() {
	var frm = document.frmMain ;
	
	frm.currentPage.value = 1;
	frm.submitted.value = 1 ;
	frm.submit();	
}

//function to open in custom window
function openWindow(url,window_name,winWidth,winHeight,fscroll,resize) {
	sWidth = screen.availWidth;
	sHeight = screen.availHeight;
	
	sLeft = (sWidth - winWidth) / 2;
	sTop = (sHeight - winHeight) / 2;
	if(fscroll == '') {fscroll = 0}
	if(resize == '') {resize = 'no'}
	
	window.open(url,window_name,"width=" + winWidth + ",height=" + winHeight + ",top=" + sTop + ",left=" + sLeft + ",toolbar=0,menubar=0,status=0,scrollbars=" + fscroll + ",resizable="+resize);
}

// allows only numeric
//eg:onKeyPress="return numericOnly(event);"
function numericOnly(e) {
	if(window.event) {
		key = e.keyCode; // for IE, e.keyCode or window.event.keyCode can be used
	}
	else if(e.which) {
		key = e.which; // netscape
	}
	else {
		return true;// no event, so pass through
	}
	
	if( ((key>45) && (key<58)) || (key == 8) ) {
		return true; 
	}
	else {
		return false;
	}
	
}

function checkAll(form,checkAll,OtherCheckbox) {
	var frm = document.forms[form];
	
	if(!frm.elements[OtherCheckbox])
		return false;

	if (frm.elements[checkAll].checked == true) 	{
		frm.elements[OtherCheckbox].checked = true;		
	}
	else {
		frm.elements[OtherCheckbox].checked = false;	
	}
	  
	for(var i = 0; i < frm.elements[OtherCheckbox].length; i++) {
		if(frm.elements[OtherCheckbox][i].type == "checkbox") {
			if(frm.elements[checkAll].checked == true) {
				frm.elements[OtherCheckbox][i].checked = true;
			}
			else {
				frm.elements[OtherCheckbox][i].checked = false;
			}
		}
	}
}

function resetChkBox(form,checkAll,OtherCheckbox) {
	var frm = document.forms[form];	
	var flag = 0;
	
	if (frm.elements[OtherCheckbox].checked == true) {
		frm.elements[checkAll].checked = true;
	}
	else {	
		flag = 1;		
	}
	for(var i = 0; i < frm.elements[OtherCheckbox].length; i++) {
		if(frm.elements[OtherCheckbox][i].checked == false) {
			flag = 1;
			break;
		}
		else {
			flag = 0;
		}
	}
	
	if(flag == 0) {
		frm.elements[checkAll].checked = true;
	}
	else {
		frm.elements[checkAll].checked = false;
	}
}

function checkEmail(strng)
{
	var error = "";
	var emailFilter=/^.+@.+\..{2,4}$/;
	if (strng == "") 
		{
		error = "You didn't enter an email-address.\n";
		}
	
	else if (!(emailFilter.test(strng))) 
		{ 
		error = "Please enter a valid email address.\n";
		}
	var illegalChars= /[\(\)\<\>\,\;\:\\\/\"\[\]]/;
	if (strng.match(illegalChars))
		{
		error = "The email address contains illegal characters.\n";
		}
	return error;
}

function checkPassword (strng) 
{
	var error = "";
	var illegalChars = /[\W_]/; 
	if (strng == "") 
	{
		error = "You didn't enter password.\n";
	}
	
	if ((strng.length < 4) || (strng.length > 15)) 
	{
  		error = "The password is of wrong length(valid length 4-15 chars).\n";
	}
	else if (illegalChars.test(strng)) 
	{
		error = "The password contains illegal characters.\n";
	}
	return error;
}

// Checks the first occurance of spaces and removes them
function trimSpaces(stringValue) {
	for(i = 0; i < stringValue.length; i++) {
		if(stringValue.charAt(i) != " ") {
			break;
		}
	}
	if(i > 0) {
		stringValue = stringValue.substring(i);
	}
	
	// Checks the last occurance of spaces and removes them
	strLength = stringValue.length - 1;
	for(i = strLength; i >= 0; i--) {
		if(stringValue.charAt(i) != " ") {
			break;
		}
	}
	if(i < strLength) {
		stringValue = stringValue.substring(0, i + 1);
	}
	
	// Returns the string after removing leading and trailing spaces.
	return stringValue;
}
function compare2Dates(date1,date2)
{
	// assumes that dates are in the format YYYY-MM-DD
	varDate = date1.split("-");
	day = trimSpaces(varDate[2]); 
	month = trimSpaces(varDate[1]); 
	year = trimSpaces(varDate[0]); 
	d1 = new Date(year,month-1,day).getTime();

	varDate = date2.split("-");
	day = trimSpaces(varDate[2]); 
	month = trimSpaces(varDate[1]); 
	year = trimSpaces(varDate[0]);
	d2 = new Date(year,month-1,day).getTime();
	 
	if (d1==0 || d2==0)
	{
		return -1;
	}
	else if (d1 > d2) 
	{
		return 1;
	}
	else if (d1 == d2) 
	{
		return 2;
	}
	return 0;
}
//convert ddm-mmm-yyyy To yyyy-mm-dd;
function ddmmmyyyyToDBdate(datestr) {
	var retarray = datestr.split("-");
	var arrMonth = new Object();
	arrMonth["Jan"] = {value:"01",text:"January"};
	arrMonth["Feb"] = {value:"02",text:"February"};
	arrMonth["Mar"] = {value:"03",text:"March"};
	arrMonth["Apr"] = {value:"04",text:"April"};
	arrMonth["May"] = {value:"05",text:"May"};
	arrMonth["Jun"] = {value:"06",text:"June"};
	arrMonth["Jul"] = {value:"07",text:"July"};
	arrMonth["Aug"] = {value:"08",text:"August"};
	arrMonth["Sep"] = {value:"09",text:"September"};
	arrMonth["Oct"] = {value:"10",text:"October"};
	arrMonth["Nov"] = {value:"11",text:"November"};
	arrMonth["Dec"] = {value:"12",text:"December"};
	var month = arrMonth[retarray[1]];
	return(retarray[2] + "-" + month.value + "-" + retarray[0]);
}

