// JavaScript Document

/* function below is an example function user use for validation. It has all the sample format that require.
function validateForm() 
{
    var why = "";
	
    why += checkEmail(myForm.email.value,"[field name]");
    why += checkPassword(myForm.password.value,"[field name]");
    why += checkUsername(myForm.username.value,"[field name]");
    why += isEmpty(myForm.notempty.value,"[field name]");
	why += verifyIP(myForm.ipaddress.value,"[field name]");
	why += verifyMacAddress(myForm.macaddress.value,"[field name]");
	why += isUrl(myForm.url.value,"[field name]");
    why += isDifferent(myForm.different.value,"[field name]");
	why += IsNumeric(myForm.different.value,"[field name]");
	
    for (i=0, n=myForm.radios.length; i<n; i++) 
	{
        if (myForm.radios[i].checked) 
		{
            var checkvalue = myForm.radios[i].value;
            break;
        } 
    }
	
    why += checkRadio(checkvalue,"[field name]");
    why += checkDropdown(myForm.choose.selectedIndex+1,"[field name]");
	
    if (why != "") 
	{
       alert(why);
       return false;
    }
	
	return true;
}
*/


//function below is use to set focus for the first field
function setFocus(formnObject)
{	
	formnObject.focus();
}
	
function isEmpty(strng,field) 
{	
	var error = ""; 
	
	if (strng.length == 0 || strng.length == "") 
	{
		error = "'"+field.toUpperCase()+"' field has not been filled in.\n";		
		//error = "'"+field.toUpperCase()+"'\n";		
	}
	
	return error;	  
}


function IsNumeric(sText,field)
{
	var ValidChars = "0123456789.?-";
	var IsNumber=true;
	var Char;
	var error = "";

	if(sText == "")
	{
		error = "'"+field.toUpperCase()+"' field has not been filled in.\n";
	}
	else
	{ 
		for (i = 0; i < sText.length && IsNumber == true; i++) 
		{ 
			Char = sText.charAt(i); 
			
			if (ValidChars.indexOf(Char) == -1) 
			{				
				error = "Only numeric values allow at '"+field.toUpperCase()+"'.\n";
			}
		}
	  
	}
	
	return error;
   
}

function isDifferent(strng) 
{
	var error = "";
	
	if (strng != "Can\'t touch this!") 
	{
		error = "You altered the inviolate text area.\n";
	}
	
	return error;
}

function isHour(strng,field) 
{	
	var error = ""; 
	

	if (strng.length == 0 || strng.length == "") 
	{
		error = "'"+field.toUpperCase()+"' field has not been filled in.\n";
	}	
	
	for (i = 0; i < strng.length; i++)
	{
		if ((strng.charAt(i) >= "A" && strng.charAt(i) <= "Z") || (strng.charAt(i) >= "a" && strng.charAt(i) <= "z")) 
		{
			error = "'"+field.toUpperCase()+"' can not have character.\n";
		}
			
	}
	
	if (strng > 24 ) 
	{					   
		error = "'"+field.toUpperCase()+"' can not more than 24.\n";
	}
	
	return error;	  
}


function isMin(strng,field) 
{	
	var error = ""; 
	
	if (strng.length == 0 || strng.length == "") 
	{
		error = "'"+field.toUpperCase()+"' field has not been filled in.\n";
	}
	
	for (i = 0; i < strng.length; i++)
	{
		if ((strng.charAt(i) >= "A" && strng.charAt(i) <= "Z") || (strng.charAt(i) >= "a" && strng.charAt(i) <= "z")) 
		error = "'"+field.toUpperCase()+"' can not have character.\n";				
	}
	
	if (strng > 60) 
	{					   
		error = "'"+field.toUpperCase()+"' can not more than 60.\n";
	}
	
	return error;	  
}

// ------------------------------------------------------------------
// isDate ( date_string, format_string )
// Returns true if date string matches format of format string and
// is a valid date. Else returns false.
// It is recommended that you trim whitespace around the value before
// passing it to this function, as whitespace is NOT ignored!
// ------------------------------------------------------------------
function isDate(val,format) 
{
	var date=getDateFromFormat(val,format);
	var error = "";
	
	if (date==0) 
	{ 
		return false; 
	}
	
	return true;
}
	
function checkEmail(strng,field)
{
	var error = "";
	var emailFilter=/^.+@.+\..{2,3}$/;
	
	//define illegal characters for email
	var illegalChars= /[\(\)\<\>\,\;\:\\\/\"\[\]]/
		
	if (strng.length == 0) 
	{		
		error = "'"+field.toUpperCase()+"' field has not been filled in.\n";
	}
	
	if (!(emailFilter.test(strng))) 	
	{ 		  
		   error = "Please enter a valid email address.\n";
	}
		
	if (strng.match(illegalChars)) 
	{
	   error = "The email address contains illegal characters.\n";
	}
	
	return error;
}

function checkSMTP(strng,field)
{
	var error = "";
	var emailFilter=/^.+@.+\..{2,3}$/;
	//var emailFilter=/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/;
	
	//define illegal characters for email
	var illegalChars= /[\(\)\<\>\,\;\:\@\\\/\"\[\]]/
		
	if (strng.length == 0) 
	{		
		error = "'"+field.toUpperCase()+"' field has not been filled in.\n";
	}
	
	if (!(emailFilter.test(strng))) 	
	{ 		  
		   error = "Please enter a valid SMTP address.\n";
	}
		
	if (strng.match(illegalChars)) 
	{
	   error = "The email address contains illegal characters.\n";
	}
	
	return error;
}

// valid selector from dropdown list

function checkDropdown(choice,field) 
{
    var error = "";
	
    if (choice == 0) 
	{
       error = "You did not choose an option from the drop-down list of '"+field.toUpperCase()+"'.\n";
    } 
	
	return error;
}

function verifyIP(IPvalue,field) 
{
	errorString = "";
	theName = field.toUpperCase();
	
	var ipPattern = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/;
	var ipArray = IPvalue.match(ipPattern);
	
	if(IPvalue == "")
	{
		errorString = errorString + theName + ': '+IPvalue+' field has not been filled in.\n';
	}
	else if (IPvalue == "0.0.0.0")
	{
		errorString = errorString + theName + ': '+IPvalue+' is a special IP address and cannot be used here.\n';
	}
	else if (IPvalue == "255.255.255.255")
	{
		errorString = errorString + theName + ': '+IPvalue+' is a special IP address and cannot be used here.\n';
	}
	else
	{	
		if (ipArray == null)
		{
			errorString = errorString + theName + ': '+IPvalue+' is not a valid IP address.\n';
		}
		else 
		{
			for (i = 0; i < 4; i++) 
			{
				thisSegment = ipArray[i];
					
				if (thisSegment > 255) 
				{
					errorString = errorString + theName + ': '+IPvalue+' is not a valid IP address.\n';
					i = 4;
				}
					
				if ((i == 0) && (thisSegment > 255)) 
				{
					errorString = errorString + theName + ': '+IPvalue+' is a special IP address and cannot be used here.\n';
					i = 4;
				}
			}
		}
	}
	
	extensionLength = 3;	
	
	return errorString;
	
}

function verifyMacAddress(strng,field)
{
	var error = "";
	var strng = strng.toUpperCase();	
	var macpattern =/([A-G0-9]{2}\:?){6}/
	
	//alert(macpattern.test(strng));
	
	if (strng.length == 0 || strng.length == "") 
	{
		error = "'"+field.toUpperCase()+"' field has not been filled in.\n";		
	}	
	else if(!macpattern.test(strng) || (strng.length >= 18))//strng.match(macpattern)
	{
		error = "'"+field.toUpperCase()+"' is not a valid MAC address.\n";
	}
	
	return error;
} 

function isUrl(s,field) 
{
	var regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
	var error = "";
	
	if(s == "")
	{
		error = "'"+field.toUpperCase()+"' field has not been filled in.\n";	
	}
	else if(!regexp.test(s))
	{
		error = "'"+field.toUpperCase()+"' is not a valid URL address.\n";
	}
	
	return error;
}



function confirmDelete() 
{	
	if (confirm("Are you sure you want to delete this record?")) 
	{		
		return true;
	}
	else
	{
		return false;
	}
}



function checkAllBox(ref) 
{
	var chkAll = document.getElementById('checkAll');
	var checks = document.getElementsByName('del[]');
	var removeButton = document.getElementById('removeChecked');
	var boxLength = checks.length;
	var allChecked = false;
	var totalChecked = 0;	
	
	if ( ref == 1 ) 
	{
		if ( chkAll.checked == true ) 
		{
			for ( i=0; i < boxLength; i++ ) 
			{
				checks[i].checked = true;
			}
		}
		else 
		{
			for ( i=0; i < boxLength; i++ ) 
			{
				checks[i].checked = false;
			}
		}
	}
	else 
	{
		for ( i=0; i < boxLength; i++ ) 
		{
			if ( checks[i].checked == true ) 
		  	{
				allChecked = true;
				continue;
		  	}
		  	else 
		  	{
				allChecked = false;
				break;
		  	}
		}
		if ( allChecked == true ) 
		{
		  	chkAll.checked = true;
		}
		else 
		{
		  	chkAll.checked = false;
		}
	}
	  
	for ( j=0; j < boxLength; j++ ) 
	{
		if ( checks[j].checked == true ) 
		{
		  	totalChecked++;
		}
	}
	
	//removeButton.value = "Remove ["+totalChecked+"] Selected";
}

function getCheckedValue(radioObj) 
{
	if(!radioObj)
	{
		return "";
	}
	
	var radioLength = radioObj.length;
	
	if(radioLength == undefined)
	{
		if(radioObj.checked)
		{
			return radioObj.value;
		}
		else
		{
			return "";
		}
	}
		
	for(var i = 0; i < radioLength; i++) 
	{
		if(radioObj[i].checked) 
		{
			return radioObj[i].value;
		}
	}
	return "";
}

function popUp(theURL,theParameters)
{
	//parameters = scrollbars=yes,resizable=yes,width=320,height=320,toolbar=no,menubar=no,location=no,directories=no
	picWin = window.open(theURL,'',theParameters);
	picWin.focus();	
}

function previewImage(theURL,theWidth,theHeight,theParameters)
{
	//parameters = scrollbars=yes,resizable=yes,width=320,height=320,toolbar=no,menubar=no,location=no,directories=no	
	
	window.open("previewpage.htm?urlpath="+theURL+"&filewidth="+theWidth+"&fileheight="+theHeight,"_blank",theParameters);
}



