//
// +-------------------------------------------------------------+
// | eXperience Form Validations
// | Version Beta 1.0
// | Updated on: June 10, 2003
// | Copyright ©2002-2003 Deepak Kamboj <deepakkamboj@hotmail.com>
// +-------------------------------------------------------------+
// | THIS IS NOT FREE SOFTWARE
// | If you have downloaded this software from a website other
// | than www.kambojsociety.com or if you have otherwise received
// | this software from someone who is not a representative of
// | this organization you are involved in an illegal activity.
// | License agreement: http://www.kambojsociety.com/license.html
// +-------------------------------------------------------------+




extArray = new Array(".gif", ".jpg", ".png");


function checkFileExtension(file)
{
   allowSubmit = false;
    //if (!file) return;
   while (file.indexOf("\\") != -1)
   file = file.slice(file.indexOf("\\") + 1);
   ext = file.slice(file.indexOf(".")).toLowerCase();
    for (var i = 0; i < extArray.length; i++)
     {
      if (extArray[i] == ext) 
       { 
         allowSubmit = true; break;
       }
     }
 
    if(allowSubmit == false){return true;}
    else{return false;}
    
} 

function textCounters(field,maxChars,spanName)
{

   var txtArea = field; //document.getElementById('DESCRIPTION');
   document.getElementById(spanName).innerHTML = maxChars + txtArea.value.length;
}



function isblank(s)  {
	if ((s == null) || (s == "")) return true;
	for(var i=0;i<s.length;i++)  {
	   var c = s.charAt(i);		
	   if((c != ' ') && (c != '\\n') && (c != '\\t')) return false;
        }
	return true;
}

// ---- Function to check whether the field contains all spaces or special chars -----

function checkChar(thefield)
{
	var theinput  = thefield.value;
	var thelength = theinput.length;
	var nospchar  = true;

	var test = new String();

	for(var k=0; k<thelength; k++)
		test = test+" ";

	if(theinput == test)
		return false;
	else
	{
		for(var i=0; i<thelength; i++)
		{
			var thechar = theinput.substring(i, i+1);

			if(thechar == " ")		
				nospchar=true;

			else if(thechar<"0" || (thechar>"9" && thechar<"A") || (thechar>"Z" && 						thechar<"a") || thechar>"z") 
			{
				nospchar=false;
				break;
			}
		}
	
		return nospchar;	
	}
}

//Value is only a number
function checkNumber(thevalue) {
ok = 0; 
for (i=0;i<thevalue.length;i++) {
   a = thevalue.substr(i,1);
   if (a != "0" && a != "1" && a != "2" && a != "3" && a != "4" && a != "5" && a != "6" && a != "7" && a != "8" && a != "9") {
   ok = 1;
   }
}
	if (ok == 0) { return false; }
	else { return true; }
}


//Value is only an Amount
function checkAmount(thevalue) {
ok = 0; 
for (i=0;i<thevalue.length;i++) {
   a = thevalue.substr(i,1);
   if (a != "0" && a != "1" && a != "2" && a != "3" && a != "4" && a != "5" && a != "6" && a != "7" && a != "8" && a != "9" && a != ".") {
   ok = 1;
   }
}
	if (ok == 0) { return false; }
	else { return true; }
}


//Value is only an Date
function checkDate(thevalue) {
ok = 0; 

for (i=0;i<thevalue.length;i++) {
   a = thevalue.substr(i,1);
   if (a != "0" && a != "1" && a != "2" && a != "3" && a != "4" && a != "5" && a != "6" && a != "7" && a != "8" && a != "9" && a != "-" && a != "/") {
   ok = 1;
   }
}

if ((thevalue.length < 8) && (thevalue.length > 10))
{
  ok = 0;
}

	if (ok == 0) { return false; }
	else { return true; }
}


function checkTelephone(thevalue) {
ok = true; 
for (i=0;i<thevalue.length;i++) {
   a = thevalue.substr(i,1);
   if (a != "0" && a != "1" && a != "2" && a != "3" && a != "4" && a != "5" && a != "6" && a != "7" && a != "8" && a != "9" && a != "-" && a != "+") 
	   {
	   ok = false;
       }
}
	if (ok == false) { return false; }
	else { return true; }
}


function onlyChar()
  {
    if(((event.keyCode>=97)&&(event.keyCode<=122)) 
        || ((event.keyCode>=65)&&(event.keyCode<=90))
        || ((event.keyCode==46)||(event.keyCode==32)))
        
           event.keyCode=event.keyCode;
    else
           event.keyCode=0;
  }
 

//This function used for numeric check 
function onlyInt()
   {
     if((event.keyCode>=48)&&(event.keyCode<=57))
     {
        event.keyCode=event.keyCode;
     }
     else
     {
       event.keyCode=0;
      
     }
  }


  //This function used for float check 
function onlyFloat()
   {
     if(((event.keyCode>=48)&&(event.keyCode<=57)) || (event.keyCode==46))
     {
        event.keyCode=event.keyCode;
     }
     else
     {
       event.keyCode=0;
      
     }
  }


function checkLength(thefield, min, max)
{
	var thelength = thefield.length;
	var goodvalue = true;
	
	if(thelength < min)
	{
		goodvalue=false;
	}
	
	if(thelength > max)
	{
		goodvalue=false;
	}
	
	return goodvalue;
}

// -------- Function to trim all extra blankspaces (leading, trailing and
//          in-midayle) from the field and set it to new acceptable format ----------

function trimAll(thefield)
{

	var thelength = thefield.length;
	var test      = new String();
	var result    = new String();

	for(var i=0; i<thelength; i++)
	{
		var thechar  = theinput.charAt(i);

		if(i>0 && i<thelength)
		{
			var thecharl = theinput.charAt(i-1);
			var thecharr = theinput.charAt(i+1);
		}

		if((i==0) || (i==thelength-1))
		{
			if(thechar != " ")
				test = test + thechar;
		}
		else
		{
			if(thechar == " ")
			{
				if((thecharl != " " && thecharr == " ") ||
				   (thecharl != " " && thecharr != " "))
				test = test + thechar;
			}
			else
				test = test + thechar;
		}
	}
		
	if(test.charAt(test.length-1) == " ")
	{
		for(j=0; j<test.length-1; j++)
			result = result + test.charAt(j);
	}
	else
	{
		for(j=0; j<test.length; j++)
			result = result + test.charAt(j);
	}
	
	thefield = result;
}	


// ------------ Function to check whether the field includes spaces -----------------

function checkSpace(thefield)
{
	var theinput  = thefield.value;
	var thelength = theinput.length;
	var nospace   = true;
	
	for(var i=0; i<thelength; i++)
	{
		var thechar = theinput.substring(i, i+1);

		if(thechar == " ")
		{
			nospace=false;
			break;
		}
	}
	return nospace;	
}

function checkEmail (thevalue) {

var checkTLD=1;
var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
var emailPat=/^(.+)@(.+)$/;
var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
var validChars="\[^\\s" + specialChars + "\]";
var quotedUser="(\"[^\"]*\")";
var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
var atom=validChars + '+';
var word="(" + atom + "|" + quotedUser + ")";
var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
var matchArray=thevalue.match(emailPat);

if (matchArray==null) {


alert("Email address seems incorrect (check @ and .'s)");
return false;
}
var user=matchArray[1];
var domain=matchArray[2];


for (i=0; i<user.length; i++) {
if (user.charCodeAt(i)>127) {
alert("Ths username in email address contains invalid characters.");
return false;
   }
}
for (i=0; i<domain.length; i++) {
if (domain.charCodeAt(i)>127) {
alert("Ths domain name in email address contains invalid characters.");
return false;
   }
}



if (user.match(userPat)==null) {

// user is not valid

alert("The username in email address doesn't seem to be valid.");
return false;
}


var IPArray=domain.match(ipDomainPat);
if (IPArray!=null) {

// this is an IP address

for (var i=1;i<=4;i++) {
if (IPArray[i]>255) {
alert("Destination IP address in email address is invalid!");
return false;
   }
}
return true;
}

// Domain is symbolic name.  Check if it's valid.
 
var atomPat=new RegExp("^" + atom + "$");
var domArr=domain.split(".");
var len=domArr.length;
for (i=0;i<len;i++) {
if (domArr[i].search(atomPat)==-1) {
alert("The domain name in email address does not seem to be valid.");
return false;
   }
}

if (checkTLD && domArr[domArr.length-1].length!=2 && 
domArr[domArr.length-1].search(knownDomsPat)==-1) {
alert("The email address must end in a well-known domain or two letter " + "country.");
return false;
}

// Make sure there's a host name preceding the domain.

if (len<2) {
alert("This email address is missing a hostname!");
return false;
}

// If we've gotten this far, everything's valid!
return true;
}

function IsValidTime(timeStr) {
// Checks if time is in HH:MM:SS AM/PM format.
// The seconds and AM/PM are optional.

var timePat = /^(\d{1,2}):(\d{2})(:(\d{2}))?(\s?(AM|am|PM|pm))?$/;

var matchArray = timeStr.match(timePat);


if (matchArray == null) {
alert("Time is not in a valid format.");
return false;
}

hour = matchArray[1];
minute = matchArray[2];
second = matchArray[4];
ampm = matchArray[6];

if (second=="") { second = null; }
if (ampm=="") { ampm = null }

if (hour < 0  || hour > 23) {
alert("Hour must be between 1 and 12. (or 0 and 23 for military time)");
return false;
}

if (minute<0 || minute > 59) {
alert ("Minute must be between 0 and 59.");
return false;
}

return true;
}

function valRadioButton(thevalue)
{
	// place any other field validations that you require here
	// validate myradiobuttons
	ok = true;
	for (i=thevalue.length-1; i > -1; i--)
	{
		if (thevalue[i].checked)
		{
			 ok = false;
       	}
	}
	if (ok == false) { return false; }
	else { return true; }
}

function isBlankCheckbox(thename)
{

		var isOk;
		var arLength=thename.length;

		isOk=false;
		if(isNaN(arLength))
		{
			if(thename.checked==true)
			{
				isOk=true;
			}
		}
		else
		{
			for(iCmbCnt=0;iCmbCnt<arLength;iCmbCnt++)
			{
				if(thename[iCmbCnt].checked==true)
				{
					isOk=true;
				}
			}
		}

		if(isOk==false)
		{
			return false;
		}
		else
		{
			return true;
		}
}



function isCheckedAny(thename)
{
var j;
j = 0;
var isOk;
		var arLength=thename.length;

		isOk=false;
		if(isNaN(arLength))
		{
			if(thename.checked==true)
			{
				isOk=true;
			}
		}
		else
		{
			for(iCmbCnt=0;iCmbCnt<arLength;iCmbCnt++)
			{
				if(thename[iCmbCnt].checked==true)
				{
					if (thename[iCmbCnt].value == "No Preference")
					{
					isOk=true;
					}
					j++;
				}
			}
		}

		if(isOk==true && j > 1)
		{
			return true;
		}
		else
		{
			return true;
		}
}


function isMoreThanFive(thename)
{
var j;
j = 0;
var isOk;
		var arLength=thename.length;

		isOk=false;
		if(isNaN(arLength))
		{
			if(thename.checked==true)
			{
				isOk=true;
			}
		}
		else
		{
			for(iCmbCnt=0;iCmbCnt<arLength;iCmbCnt++)
			{
				if(thename[iCmbCnt].checked==true)
				{
					j++;
				}
			}
		}

		if(j > 5)
		{
			return false;
		}
		else
		{
			return true;
		}
}







//function to display the pop up window in center
function oConfigWin(url,w,h) {

    if (!w) w = 400;
    if (!h) h = 500;

    if (document.all)
        var xMax = screen.width, yMax = screen.height;
    else
        if (document.layers)
            var xMax = window.outerWidth, yMax = window.outerHeight;
        else
            var xMax = w, yMax=h;
 
    var xOffset = (xMax - w)/2, yOffset = (yMax - h)/2;
 
    config =  window.open(url,'','width='+w+',height='+h+',screenX='+xOffset+',screenY='+yOffset+',top='+yOffset+',left='+xOffset+',directories=no,location=no,menubar=no,scrollbars=no,status=no,toolbar=no,resizable=no');
}

// #### right click of mouse disabled ### 
function click(e) {
	if (document.all) {
		if (event.button==2||event.button==3) {
			oncontextmenu='return false';
		}
	}
	if (document.layers) {
		if (e.which == 3) {
			oncontextmenu='return false';
		}
	}
}
if (document.layers) {
	document.captureEvents(Event.MOUSEDOWN);
}
document.onmousedown=click;
document.oncontextmenu = new Function("return false;")