//general - all page functions

function IsNumeric(sText)
{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
}

function isDate (value)
{
return (!isNaN (new Date (value).getYear () ) ) ;
}

function isTime (tText)
{
var timeOfDay;
if(tText.length==0) {return 0;}
else{
	var timeArray = tText.split(" ");
	if(timeArray.length<=1) {return 0}
	else{
		timeOfDay = timeArray[1].toLowerCase();
		if(timeOfDay !="am" && timeOfDay !="pm") {return 0;}
		else{
			var clockArray = timeArray[0].split(":");
			if(clockArray.length!=2) {return 0;}
			else {
				if(!IsNumeric(clockArray[0]) || !IsNumeric(clockArray[1])) {return 0;}
				else {
					if(clockArray[0]>12 || clockArray[0]<1 || clockArray[1]>60 || clockArray[0]<0) {return 0;}
					else return 1;
				}	
			}	
		}		
	}	
}	
}

function isEmail(str) 
{
	
var at="@"
var dot="."
var lat=str.indexOf(at)
var lstr=str.length
var ldot=str.indexOf(dot)
//alert("len:" + lstr + ", @:" + lat + ",dot:" + ldot);
if (str.indexOf(at)==-1){
   alert("Invalid E-mail address, please revise")
   return false
}

if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==(lstr-1)){
   alert("Invalid E-mail address, please revise")
   return false
}

if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==(lstr-1)){
	alert("Invalid E-mail address, please revise")
	return false
}

 if (str.indexOf(at,(lat+1))!=-1){
	alert("Invalid E-mail address, please revise")
	return false
 }

 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
	alert("Invalid E-mail address, please revise")
	return false
 }

 if (str.indexOf(dot,(lat+2))==-1){
	alert("Invalid E-mail address, please revise")
	return false
 }

 if (str.indexOf(" ")!=-1){
	alert("Invalid E-mail address, please revise")
	return false
 }

 return true					
}

function naturalSort (a, b) {
    // setup temp-scope variables for comparison evauluation
    var x = a.toString().toLowerCase() || '', y = b.toString().toLowerCase() || '',
        nC = String.fromCharCode(0),
        xN = x.replace(/([-]{0,1}[0-9.]{1,})/g, nC + '$1' + nC).split(nC),
        yN = y.replace(/([-]{0,1}[0-9.]{1,})/g, nC + '$1' + nC).split(nC),
        xD = (new Date(x)).getTime(), yD = (new Date(y)).getTime();
    // natural sorting of dates
    if ( xD && yD && xD < yD )
        return -1;
    else if ( xD && yD && xD > yD )
        return 1;
    // natural sorting through split numeric strings and default strings
    for ( var cLoc=0, numS = Math.max( xN.length, yN.length ); cLoc < numS; cLoc++ )
        if ( ( parseFloat( xN[cLoc] ) || xN[cLoc] ) < ( parseFloat( yN[cLoc] ) || yN[cLoc] ) )
            return -1;
        else if ( ( parseFloat( xN[cLoc] ) || xN[cLoc] ) > ( parseFloat( yN[cLoc] ) || yN[cLoc] ) )
            return 1;
    return 0;
}

function setFocus() {
window.focus();	
}

//form code 

function formInputBlur(obj) {
	//alert(obj.value.length);
	//alert(obj.style.background);
	if(obj.value.length>0 && obj.style.backgroundColor=="red") {
		obj.style.background="white";
	//	obj.style.border="1px solid #7f9db9";
	//	obj.style.padding="2px";
	}
	//obj.style.background="white";
	//alert("~" + obj.value + "~");	
}

function formInputFocus(obj) {
	//alert(obj.style.background);
	//obj.style.background="white";
	//obj.style.border="3px solid blue"
}

//end form code

// end general - all page functions

// items for index page



var myWindow;

function processLogin() {

	var url = "http://206.191.32.244/easyad/user_admin/processlogin.asp?login=" + document.getElementById("login").value + "&password=" + document.getElementById("password").value;
	//myWindow=window.open(url,"loginPage");
	window.open(url,"loginPage","toolbar=no,location=no,directories=no,status=no,scrollbars=yes,menubar=no,resizable=yes,copyhistory=no, top=0, left=0,width=" + (screen.availWidth-14) + ",height=" + (screen.availHeight-30));

}


function loadPage(pageURL) {
	document.getElementById("contentFrame").src = pageURL + ".html";
	var flashBannerPages = "home,products,services,markets_served,portfolio";
	if(flashBannerPages.match(pageURL)!=null) document.getElementById("flashBanner").src = pageURL + "_flash.html";
}

var baseHeight = 445;

function resize() {
//	var parentURL = parent.document.location.href;
//	if(parentURL!="http://www.aditudemedia.com/" && parentURL!="http://www.aditudemedia.com/index.html" && parentURL.match("contentURL")==null) {
//		var pageName = parentURL.split("/");
//		var contentPage = pageName[pageName.length-1].replace(".html","");
//		document.location.href = "http://www.aditudemedia.com/index.html?contentURL=" + contentPage;
//	}	
//	else {	
		var pageHeight = parseInt(document.getElementById("leftSide").offsetHeight);
		if(pageHeight<baseHeight) pageHeight = baseHeight;
		parent.document.getElementById("content").style.height = pageHeight + "px";		
		parent.document.getElementById("contentFrame").height = pageHeight + "px";
		document.getElementById("leftSide_border").style.height = (pageHeight-40) + "px";
//	}
	
}

function home_resize() {
	
//	var parentURL = parent.document.location.href;
//	if(parentURL!="http://www.aditudemedia.com/" && parentURL!="http://www.aditudemedia.com/index.html" && parentURL.match("contentURL")==null) {
//		var pageName = parentURL.split("/");
//		var contentPage = pageName[pageName.length-1].replace(".html","");
//		document.location.href = "http://www.aditudemedia.com/index.html?contentURL=" + contentPage;
//	}	
//	else {
		var leftHeight = parseInt(document.getElementById("home_leftSide").offsetHeight);
		var rightHeight = parseInt(document.getElementById("home_rightSide").offsetHeight);
		var pageHeight;
		if(leftHeight>rightHeight) pageHeight = leftHeight;
		else pageHeight = rightHeight;
		if(pageHeight>baseHeight) pageHeight = pageHeight + 10; //bottom gap of 10 for larger pages
		else pageHeight = baseHeight;
		parent.document.getElementById("content").style.height = pageHeight + "px";		
		parent.document.getElementById("contentFrame").height = pageHeight+ "px";
		document.getElementById("home_rightSide_border").style.height = (pageHeight-40) + "px";
		document.getElementById("home_leftSide_border").style.height = (pageHeight-40) + "px";
//	}
}

// end items for index page

//for inquiry form page:

function validate(frmName) {
	var x=document.getElementById("inquiryForm");
	var error=0;
	for (var i=0;i<x.length;i++)
	{
	  if(x.elements[i].value=="" || x.elements[i].value==null) { 
	  	if(error==0) alert("Highlighted fields are missing data, please type in required information");
		x.elements[i].style.backgroundColor="red";
		error=1;
	  } 	 
	}
	
	if(error==0) if(!isEmail(document.getElementById("email").value)) error=1;
	  

	if(error==1) return false;
	else return true;
}

function clearForm(frmName) {
	var x=document.getElementById(frmName);
	for (var i=0;i<x.length;i++)
	{
		if(x.elements[i].type!="hidden" && x.elements[i].type!="submit" && x.elements[i].type!="button") x.elements[i].value="";
		if(x.elements[i].style.backgroundColor=="red") x.elements[i].style.backgroundColor="white";
	} 	 
}

	