// common js functions
// 
// Copyright (c) 2007 Tribal DDB Canada
// Barry Frith & James Marshall www.tribalddb.ca
// 
// v4

// image swapping
var origImg;

function swapToImg( id, img ) {
	document.getElementById( id ).src = img;
}

function swap( id, img ) {
	document.getElementById( id ).src = img.src;
}

function navOver( id ) {
	origImg = document.getElementById( "nav_" + id ).src;
	document.getElementById( "nav_" + id ).src = eval( "nav_" + id + "_f2.src" );
	if ( document.getElementById( "sub_" + id ) ) {
		document.getElementById( "sub_" + id ).style.display = "block";
	}
}

function navOut( id ) {
	document.getElementById( "nav_" + id ).src = origImg;
	if ( document.getElementById( "sub_" + id ) ) {
		document.getElementById( "sub_" + id ).style.display = "none";
	}
}

/* Return true if str is a list of comma separated numbers.
 * Numbers are to be at least 2 digits in length.
 * Unlimited number of values allowed.
 */

function isCSN( alphaField, errMsg ) {
	if ( alphaField.value.match(/^[0-9]+(,[0-9]+)*$/) )
	{
		return true;
	}else{
		alert(errMsg);
		alphaField.focus();
		alphaField.select();
		return false;
	}
}

function isNumeric(alphaField, errMsg ) {
	if (isNaN(alphaField.value)==false  && alphaField.value > 0)
	{
		return true;
	}else{
		alert(errMsg);
		alphaField.focus();
		alphaField.select();
		return false;
	}
}

function isNumericMaxLength(alphaField, errMsg, maxLength ) {
	if (isNaN(alphaField.value)==false  && alphaField.value > 0 && alphaField.value.length <= maxLength )
	{
		return true;
	}else{
		alert(errMsg);
		alphaField.focus();
		alphaField.select();
		return false;
	}
}


function isName( alphaField, errMsg ) {
	if ( alphaField.value.match(/^[A-Za-z\'\- ]+$/) )
	{
		return true;
	}else{
		alert(errMsg);
		alphaField.focus();
		alphaField.select();
		return false;
	}
}

function isEmail( alphaField, errMsg ) {
	if ( alphaField.value.search(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/) != -1 ) {
		return true;
	} else {
		alert(errMsg);
		alphaField.focus();
		alphaField.select();
		return false;
	}
}

function isEmailMulti( alphaField, errMsg ) {
	// email addresses separated by commas
	if ( alphaField.value.search(/^(\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+(,( )?)?)+$/) != -1 ) {
		return true;
	} else {
		alert(errMsg);
		alphaField.focus();
		alphaField.select();
		return false;
	}
}


function isEmpty( alphaField, errMsg ) {
	// email addresses separated by commas
	if ( alphaField.value != "") {
		return true;
	} else {
		alert(errMsg);
		alphaField.focus();
		alphaField.select();
		return false;
	}
}

function hasValues( alphaField, errMsg ) {
	// email addresses separated by commas
	if ( alphaField.value == "") {
		return false;
	} else {
		alert(errMsg);
		alphaField.focus();
		alphaField.select();
		return true;
	}
}


function isSelectEmpty( alphaField, errMsg ) {
	// email addresses separated by commas
	if ( alphaField.value != "") {
		return true;
	} else {
		alert(errMsg);
		alphaField.focus();
		return false;
	}
}

function isDate(year, month, day) {
	// month argument must be in the range 1 - 12
	month = month - 1; // javascript month range : 0- 11
	var tempDate = new Date(year,month,day);
	if( (tempDate.getYear() == year) && (month == tempDate.getMonth()) && (day == tempDate.getDate()) )
	{
		return true;
	}
	else
	{
		return false;
	}
}

function isAdminDate( alphaField, errMsg ) {
	// 3+3+4 digit
	if( alphaField.value.match(/^\d\d\d\d\-\d\d\-\d\d$/) )
	{
	return true;
	}else{
	alert(errMsg);
	alphaField.focus();
	alphaField.select();
	return false;
	}
}


function isPhone( alphaField, errMsg ) {
	// 3+3+4 digit
	if( alphaField.value.match(/^\d\d\d\-\d\d\d\-\d\d\d\d$/) )
	{
	return true;
	}else{
	alert(errMsg);
	alphaField.focus();
	alphaField.select();
	return false;
	}
}

function fixPhone( phone ) {
	var num = phone.value;
	var newnum = "";
	var output = "";
	// strip out non-numbers
	for ( var i = 0; i < num.length; i++ )
		if ( num.charAt( i ).match(/\d/) )
			newnum += num.charAt( i );
	if ( newnum ) {
		// rebuild number with hyphen
		for ( var i = 0; i < newnum.length; i++ ) {
			if ( i == 3 || i == 6 )
				output += "-";
			else if ( i == 10 )
				output += " ";
			output += newnum.charAt( i )
		}
	}
	// return value
	phone.value = output;
}

function isZip( alphaField, errMsg ) {
	// 5 digit zips
	if( alphaField.value.match(/^\d\d\d\d\d$/) )
	{
		return true;
		}
	// 5+4 digit zips
	if( ( alphaField.value.match(/^\d\d\d\d\d\d\d\d\d$/) ) || ( zip.match(/^\d\d\d\d\d\-\d\d\d\d$/) ) )
	{
		return true;
	}else{	
	alert(errMsg);
	alphaField.focus();
	alphaField.select();
	return false;
	}
}

function isPostalCode( alphaField, errMsg ) {
	// canadian postal codes (6 or 7 characters)
		pc=alphaField.value;
		if( ( pc.match(/^[A-Za-z]\d[A-Za-z]\d[A-Za-z]\d$/) ) || ( pc.match(/^[A-Za-z]\d[A-Za-z] \d[A-Za-z]\d$/) ) )
		{
		return true;
		}else{
		alert(errMsg);
		alphaField.focus();
		alphaField.select();
		return false;
		}
}

function isPostalCodeorZip( alphaField, errMsg )
{
		if( (alphaField.value.match(/^[A-Za-z]\d[A-Za-z]\d[A-Za-z]\d$/) ) || ( alphaField.value.match(/^[A-Za-z]\d[A-Za-z] \d[A-Za-z]\d$/) ) )
		{
			return true;
		}else{
			// 5 digit zips
			if( alphaField.value.match(/^\d\d\d\d\d$/) )
			{
				return true;
			}
			// 5+4 digit zips
			if( ( alphaField.value.match(/^\d\d\d\d\d\d\d\d\d$/) ) || ( alphaField.value.match(/^\d\d\d\d\d\-\d\d\d\d$/) ) )
			{
				return true;
			}else{	
				alert(errMsg);
				alphaField.focus();
				alphaField.select();
				return false;
			}
		}
}



function fixPostalCode( pc ) {
	var newpc = "";
	var output = "";
	// strip out non-alphanumeric
	for ( var i = 0; i < pc.value.length; i++ )
		if ( pc.value.charAt( i ).match(/\w/) )
			newpc += pc.value.charAt( i );
	if ( newpc ) {
		// rebuild number with space
		for ( var i = 0; i < 6; i++ ) {
			output += newpc.charAt( i )
			if ( i == 2 )
				output += " ";
		}
	}
	// return value in uppercase
	pc.value = output.toUpperCase();
}

// popup windows
function popup( url, name, w, h ) {
	var x = (screen.width - w) / 2;
	var y = (screen.availHeight - h) / 2;
	var page = window.open(url,name,"toolbar=no,location=no,directories=no,status=no,menubar=no,resizable=no,scrollbars=no,width=" + w + ",height=" + h + ",screenX=" + x + ",screenY=" + y + ",top=" + y + ",left=" + x + "");
	page.focus();
	return page;
}

// popup windows
function popupScroll( url, name, w, h ) {
	var x = (screen.width - w) / 2;
	var y = (screen.availHeight - h) / 2;
	var page = window.open(url,name,"toolbar=no,location=no,directories=no,status=no,menubar=no,resizable=no,scrollbars=yes,width=" + w + ",height=" + h + ",screenX=" + x + ",screenY=" + y + ",top=" + y + ",left=" + x + "");
	page.focus();
	return page;
}

function isSku( alphaField, errMsg ) {
	// 3+3+4 digit
	if( alphaField.value.match(/^\d\d\d\d\_\d\d\d\d\d$/) )
	{
	return true;
	}else{
	alert(errMsg);
	alphaField.focus();
	alphaField.select();
	return false;
	}
}

//*** < $1000
function isPrice( alphaField, errMsg ) {
	// 3+3+4 digit
	if(alphaField.value.match(/^\d{1,3}\.\d{2}$/))
	{
	return true;
	}else{
	alert(errMsg);
	alphaField.focus();
	alphaField.select();
	return false;
	}
}

function trim(strToTrim) 
{
	return strToTrim.replace(/^(\s+)?(.*\S)(\s+)?$/, '$2');
}

function checkAlphaNumeric(alphaField, illegalChars)
	{
		for(var j=0; j< alphaField.length; j++)
			{
			theChar = alphaField.charAt(j);

				if(illegalChars.indexOf(theChar) > -1)
				{
					return false;
				} 
			}
		return true;
	}

function validateAlphaNumeric(alphaField, minLength, maxLength, errorMsg)
{
illegalChars="\"()\\";
alphaValue = trim(alphaField.value);

if(((alphaValue.length > 0) && (!checkAlphaNumeric(alphaValue,illegalChars)) || (alphaValue.length > maxLength) || (alphaValue.length < minLength))){
	
		if(alphaValue.length < minLength)
		{
		alert(errorMsg);
		}else{
		alert(errorMsg + "(" + illegalChars + ")" );
		}
	
	alphaField.focus();
	alphaField.select();
	return false;
	}else{
	return true;
	}
}

function validateDDChosen(ddName, errorMsg)
{
	if(ddName.selectedIndex == -1)
	{
	alert(errorMsg);
	ddName.focus();
	return false;
	}else{
	return true;
	}
}


function isPageInFrameSet()
{
	if(top.location == self.location)
	{
	return false;
	}else{
	return true;
	}
}

//***************************************
// Ajax Code, does get or post on passed Query String
// Calls defined function
/*

Ajax2Function("GET","http://ethel/playground/mainsite/site/ajaxTests/threedpage.html","","getHTML"); 

function getHTML()
{
     if (req.readyState == 4) {
        response = req.responseText;
        document.getElementById("test").innerHTML = response; 
        processInlineJavascript(response); 
	}
}
*/

var req;

function Ajax2Function(serverFunction, url, formStr, callBackFunction) 
{
    // branch for native XMLHttpRequest object
    if (window.XMLHttpRequest)
     {
        req = new XMLHttpRequest();

    } else if (window.ActiveXObject) 
    {
        req = new ActiveXObject("Microsoft.XMLHTTP");
    } 
        
        if (req)
        {
		req.onreadystatechange = eval(callBackFunction);

				if(serverFunction=="POST")
				{
					req.open("POST", url, true);
					req.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
					req.setRequestHeader("Content-length", formStr.length);
					req.setRequestHeader("Connection", "close");
					req.send(formStr);
				}else{       
					req.open("GET", url+"?"+formStr, true);
					req.send(null);
				}
        }
}

// *** Strips out javascript from between tags and executes it
function processInlineJavascript(response)
{
    var re = /<script.*?>([\s\S]*?)<\//igm;
     while (match = re.exec(response)) { 
       eval(match[1]); 
     }
}
