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

//function to open image in custom window
function openImage(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'}
	
	winId = window.open('',window_name,"width=" + winWidth + ",height=" + winHeight + ",top=" + sTop + ",left=" + sLeft + ",toolbar=0,menubar=0,status=0,scrollbars=" + fscroll + ",resizable="+resize);
	
	with (winId.document) {
		write('<html><title>Picture Viewer</title><head><style>body {margin-left:0px;margin-top: 0px;margin-right: 0px;margin-bottom: 0px;}</style></head>');
		write('<body onLoad="window.focus();" >');
		write('<div align="center"><img src="'+url+'"></div>');
		write('</body></html>');
		close();
	}
	
}

// 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 performAction(form,action,checkAll,OtherCheckbox) {
	var frm = document.forms[form];	
	
	var flag = 0;
	
	if (frm.elements[OtherCheckbox].checked == true) {		
		flag = 1;
	}
	
	for(var i = 0; i < frm.elements[OtherCheckbox].length; i++) {
		if(frm.elements[OtherCheckbox][i].checked == true) {		
			flag = 1;
			break;
		}
	}
	
	if(flag == 0) {
		alert("Please select at least one item to "+action+".");
		return false;
	}
	else {
		var result = confirm("Are you sure to delete the selected Item(s)?");	
		if (result != "") {
			frm.form_action.value = action ;
			frm.submitted.value = 1 ;		
			frm.submit();			
		}	
	}
}

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 limitText used to limit text in a textarea
function limitText(field,maxlimit,remark) {
	if (field.value.length > maxlimit) {
		field.value = field.value.substring(0, maxlimit);
	}
	else {
		if(remark) {
			remark.value = maxlimit - field.value.length;
		}
	}	
}
