function openNewWindow (path, w, h, mnbar, resize, tbar, sbars, X, Y) {
    msgWindow = window.open(path, "newWin", "menubar="+mnbar+",width="+w+",height="+h+",resizable="+resize+",toolbar="+tbar+",scrollbars="+sbars+",screenX="+X+",screenY="+Y);
    msgWindow.focus();
}

function empty(x) { 
    if (x.length > 0) {
	return false; 
    }
    return true;    
}
function strip(x) {
    if (x.length < 1) { 
	return x;
    } 
    return x.replace(/^\s+|\s+$/g,'');
}

function checkMapForKey(map, key) {
    //alert(map[key]);
    if ("" + map[key] == "undefined") {
	//alert("The key " + key + " doesn't exist");
		return false;
    } else {
	//alert(key + " is here!");
		return true;
    }
}   
	
function validateZip(field) {
	var zip = strip(field.value);
	var filter5 = /[0-9]{5}$/;
     	
 	if (zip.length != 5 ) {
 		return false;
 	} else {
 		if (!filter5.test(zip)) {
 			return false;
 		}
 	}
	return true;
}   
        
function validatePhone(field) {
 	var phone = strip(field.value);
 	var filter = /^[1-9]\d{2}-\d{3}-\d{4}$/;
 	if (phone.length < 12 ) {
 		return false;
 	} else {
		if (!filter.test(phone))
			return false;
 	}
	return true;
}
 
function validateCreditCard (field, text){
	var cc = strip(field.value);
	var filter = /[0-9]{16}$/;
     	
 	if (cc.length != 16 ) {
 		return false;
 	} else {
 		if (!filter.test(cc)) {
 			return false;
 		} else {
 			if ((text == 'Master Card' && cc.substring(0,1) != '5') ||
 				(text == 'Visa' && cc.substring(0,1) != '4')) {
 				return false;	
 			}
 		}
 	}
	return true;
}

function validateCVV(field) {
	var cvvValue = strip(field.value);
	var filter3 = /[0-9]{3}$/;
	var filter4 = /[0-9]{3}$/;
     	
 	if (cvvValue.length != 3 && cvvValue.length != 4 ) {
 		return false;
 	} else {
 		if (!filter3.test(cvvValue) && !filter4.test(cvvValue)) {
 			return false;
 		}
 	}
	return true;

}
 
function validateEmail (field){
	var filter=/^\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,3}$/;
	if (!filter.test(field.value)) {
		return false;
	}
	return true;
}

///// Date Validation /////////////////
function validateDate(field) {
    var filter=/^\d{2}(\/)\d{2}\1\d{4}$/;
    //check to see if in correct format
    if (!filter.test(field.value) || empty(strip(field.value))) {
	field.focus();
	field.select();
	return false; //doesn't match pattern, bad date
    } else {
	//split date into month, day, year
	var arrayDate=field.value.split(RegExp.$1);
	var intDay=parseInt(arrayDate[1],10); 
	var intYear=parseInt(arrayDate[2],10);
	var intMonth=parseInt(arrayDate[0],10);
	//check for valid month
	if (intMonth > 12 || intMonth < 1) {
	    field.focus();
	    field.select();
	    return false;
	}
	//create a lookup for months not equal to Feb.
	var arrayLookup = {'01':31, '03':31, '04':30, '05':31, '06':30, '07':31,
	                   '08':31, '09':30, '10':31, '11':30, '12':31}
	//check if month value and day value agree
	if (arrayLookup[arrayDate[0]] != null) {
	    if (intDay <= arrayLookup[arrayDate[0]] && intDay != 0) {
		return true; //found in lookup table, good date
	    }
	}
	//check for February
	var booLeapYear = (intYear % 4 == 0 && (intYear % 100 != 0 || intYear % 400 == 0));
	if (((booLeapYear && intDay <= 29) || (!booLeapYear && intDay <=28)) && intDay !=0) {
	    return true; //Feb. had valid number of days
	}
    }
    field.focus();
    field.select();
    return false; //any other values, bad date
}

///// Current Date Validation /////////
function validDate(field) {
	var montharray=new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
	var today=new Date();
	var todayy=today.getYear();
	var m  = field.value.substring(0,2);
	var d  = field.value.substring(3,5);
	var yr = field.value.substring(6,10);
	
	if ((navigator.appName == "Microsoft Internet Explorer") && (todayy < 2000))		
	todayy="19" + todayy;
	if (navigator.appName == "Netscape")
	todayy=1900 + todayy;
	
	var todaym=today.getMonth();
	var todayd=today.getDate();
	var todaystring=montharray[todaym]+" "+todayd+", "+todayy;
	var Dstring=montharray[m-1]+" "+d+", "+yr;
	var diff=(Math.round((Date.parse(todaystring)-Date.parse(Dstring))/(24*60*60*1000))*1);
	
	if (diff > 0) {
		field.focus();
		field.select();
		return false;
	}
	return true;
}

function checkShortDate(field) {
	var err = false;
	var temp;
	var valid = '0123456789/';
	
	var date = field.value;
		
	if (date.length != 7) {
		err = true;
	}

	var month = date.substring(0, 2)// month
	var slash = date.substring(2, 3)// '/'
	var year = date.substring(3, 7)// year

 	//basic error checking
	if (month < 01 || month > 12) {
		err = true;
	}
	if (slash != '/') {
		err = true;
	}
	if (year < 01 || year > 9999) {
		err = true;
	}
	//basic error checking to validate only numbers and "/" are present
	for (var i=0; i<date.length; i++) {
		temp = "" + date.substring(i, i+1);
		if (valid.indexOf(temp) == "-1") {
			err = true; 
		}
	}

	if (err) {
		return false;
	} else {
		return true;
	}
}

function isValidChars(str, validChars) {
	for (var i=0; i < str.length; i++) {
		var thisChar = str.charAt(i);
		if (validChars.indexOf(thisChar) == -1) {
			return false;
		}
	}
	return true;
}
