﻿// JScript File
function setClass(){
var theinputs = document.getElementsByTagName("input");
var i;
for(i = 0; i < theinputs.length; i++)
{
if(theinputs[i].type == "radio" || theinputs[i].type == "checkbox")
{
theinputs[i].className = 'radio';
}
if(theinputs[i].type == "text" || theinputs[i].type == "password")
{
theinputs[i].className = 'text';
}
if(theinputs[i].type == "submit" || theinputs[i].type == "reset")
{
theinputs[i].className = 'submit';
}
if(theinputs[i].type == "file")
{
theinputs[i].className = 'file';
}
}
}
window.onload = setClass;

function ltrim ( s )
{
	return s.replace( /^\s*/, "" );
}

function rtrim ( s )
{
	return s.replace( /\s*$/, "" );
}

function trim ( s )
{
	return rtrim(ltrim(s));
}

function fnCheckBlankField(p_ObjControl, p_StrErrMsg)
{
    if(trim(p_ObjControl.value) == "")
    {
        alert(p_StrErrMsg);
        p_ObjControl.focus();
        return false;
    }
return true;
}

function chkInteger(objCrtl)
{
	var objRegExp  =  /(^\d\d*$)/;
	var val = trim(objCrtl.value);
	objCrtl.value = val;	
	if(!objRegExp.test(val))
	{
		alert("Please enter valid integer data in this field.");
		objCrtl.focus();
		objCrtl.select();
		return false;
	}
return true;
}

function chkNumeric(objCrtl)
{
	var objRegExp  =  /(^(-)?\d+(\.\d\d)?$)/;
	var val = trim(objCrtl.value);
	objCrtl.value = val;	
	if(!objRegExp.test(val))
	{
		alert("Please enter valid numeric data in this field.");
		objCrtl.focus();
		objCrtl.select();
		return false;
	}
return true;
}



function check_img(img_file, specs)
{
	if (img_file.value != '' && !/\.(JPG|JPEG|GIF)$/i.test(img_file.value))
	{
		var sMsg = '';
		var badtype = img_file.value.match(/\.\w+$/i);
		if (badtype == null)
		sMsg += 'Invalid file type.';
		else sMsg += 'You have selected an content file of type "' + badtype + '" to upload.'; 
		sMsg += '\nOnly the following file types are permitted:';
		sMsg += '\n\n\t.JPG\n\t.JPEG\n\t.GIF\n\nThank you.';
		alert(sMsg);
		img_file.focus();
		img_file.value = "";
		return false;
	}
	var imgObj = new Image();
	var crtlObj = img_file;
	imgObj.onload = function()
	{
		report(this, specs, crtlObj);
	}
	imgObj.src = img_file.value;
}

function report(imgObj, specs, crtlObj)
{
	var specsArray = specs.split("x")	
	if (imgObj.width != parseInt(specsArray[0]) || imgObj.height != parseInt(specsArray[1]))
	{
		alert('That image size is not ' + specs + ', the required size. Please choose another.');
		crtlObj.value = "";
		crtlObj.select();
	return false;
	}
	else return true;
}

function fnValidateEmail( strValue) {
/************************************************
DESCRIPTION: Validates that a string contains a
  valid email pattern.

 PARAMETERS:
   strValue - String to be tested for validity

RETURNS:
   True if valid, otherwise false.

REMARKS: Accounts for email with country appended
  does not validate that email contains valid URL
  type (.com, .gov, etc.) or valid country suffix.
*************************************************/
//var objRegExp = /(^[a-z]([a-z_\-\.]*)@([a-z_\-\.]*)([.][a-z]{3})$)|(^[a-z]([a-z_\-\.]*)@([a-z_\-\.]*)(\.[a-z]{2,3})(\.[a-z]{2})*$)/i;
  //check for valid email
  
  var objRegExp = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
  //check for valid email
return objRegExp.test(strValue);
}

function fnValidateUSZip( strValue ) {
/************************************************
DESCRIPTION: Validates that a string a United
  States zip code in 5 digit format or zip+4
  format. 99999 or 99999-9999

PARAMETERS:
   strValue - String to be tested for validity

RETURNS:
   True if valid, otherwise false.

*************************************************/
var objRegExp  = /(^\d{5}$)|(^\d{5}-\d{4}$)/;

//check for valid US Zipcode
return objRegExp.test(strValue);
}


