var isIE = 0;
if(document.all){
	isIE = 1;
}
function openNewWindow(URLtoOpen, windowName, windowFeatures) {
	var newWindow = window.open(URLtoOpen, windowName, windowFeatures);
}
/* FORM Validation */
var bad_words = new Array();
//trim
function Trim(TRIM_VALUE){
	if(TRIM_VALUE.length < 1){
		return"";
	}
	TRIM_VALUE = RTrim(TRIM_VALUE);
	TRIM_VALUE = LTrim(TRIM_VALUE);
	if(TRIM_VALUE==""){
		return "";
	}else{
		return TRIM_VALUE;
	}
} //End Function

function RTrim(VALUE){
	var w_space = String.fromCharCode(32);
	var v_length = VALUE.length;
	var strTemp = "";
	if(v_length < 0){
		return"";
	}
	var iTemp = v_length -1;
	
	while(iTemp > -1){
		if(VALUE.charAt(iTemp) == w_space){
		}else{
			strTemp = VALUE.substring(0,iTemp +1);
			break;
		}
		iTemp = iTemp-1;
	} //End While
	return strTemp;

} //End Function

function LTrim(VALUE){
	var w_space = String.fromCharCode(32);
	if(v_length < 1){
		return"";
	}
	var v_length = VALUE.length;
	var strTemp = "";
	
	var iTemp = 0;
	
	while(iTemp < v_length){
		if(VALUE.charAt(iTemp) == w_space){
		}else{
			strTemp = VALUE.substring(iTemp,v_length);
			break;
		}
		iTemp = iTemp + 1;
	} //End While
	return strTemp;
} //End Function

Array.prototype.contains = function (element) {
    for (var i = 0; i < this.length; i++) {
        if (this[i].toLowerCase() == element.toLowerCase()) {
            return true;
        }
    }
    return false;
};
var isEmail = /[a-z0-9\._-]+@+[a-z0-9\._-]+\.+[a-z]{2,6}$/;
var isNumber = /[0-9]/;
var isCAPostalCode = /^\D{1}\d{1}\D{1}\-?\d{1}\D{1}\d{1}$/;
var isUSPostalCode = /(^\d{5}$)|(^\d{5}-\d{4}$)/;
var isDate = /^\d{1,2}(\-|\/|\.)\d{1,2}\1\d{4}$/; //mm/dd/yyyy
var isTime = /^([1-9]|1[0-2]):[0-5]\d(:[0-5]\d(\.\d{1,3})?)?$/; //HH:MM, HH:MM:SS, HH:MM:SS.mm
var isIP = /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/;
var isMoney = /^((\$\d*)|(\$\d*\.\d{2})|(\d*)|(\d*\.\d{2}))$/;
var isSSN = /^\d{3}\-?\d{2}\-?\d{4}$/;
var isCASIN = /^\d{9}$/;//canadian ssn
function validate(myform) {
	var errorList = '';	
	for(var w=0; w < myform.length; w++){
		if(myform[w].id.substring(0,4) == 'req_') {
			if(myform[w].name == 'email_address') {
				if(!isEmail.test(Trim(myform[w].value))) {
					errorList += myform[w].name + ' must be a valid email.'+"\n";
				}else if(Trim(myform[w].value) != Trim(myform.confirm_email_address.value)){
					errorList += 'Confirm Email does not match Email Address.'+"\n";
				}
			}else if(myform[w].name == 'password'){
				if(Trim(myform[w].value).length < 3){
					errorList += myform[w].name + ' must be at least 3 characters long.'+"\n";
				}else if(Trim(myform[w].value) != Trim(myform.confirm_password.value)){
					errorList += 'Confirm Password does not match Password.'+"\n";
				}
			}else if(myform[w].name == 'zip'){
				if(!isUSPostalCode.test(Trim(myform[w].value)) && !isCAPostalCode.test(Trim(myform[w].value))){
					errorList += myform[w].name+' must be a valid postal code.'+"\n";
				}
			}else if(myform[w].name == 'billing_zip'){
				if(!isUSPostalCode.test(Trim(myform[w].value)) && !isCAPostalCode.test(Trim(myform[w].value))){
					errorList += myform[w].name+' must be a valid postal code.'+"\n";
				}
			}else{
				if(Trim(myform[w].value) == '' || (myform[w].type == 'checkbox' && !myform[w].checked)) {
					errorList += myform[w].name + ' is a required field.'+"\n";
				}else{
				}
			}
		}else{
		}
	}
	if(errorList.length>0){
		alert(errorList);
		return(false);
	}else{
		return(true);
	}
}
/* Correct PNGs in IE */
function correctPNG(){
	for(var i=0; i<document.images.length; i++){
		var img = document.images[i];
		var imgName = img.src.toUpperCase();
		if(imgName.substring(imgName.length-3, imgName.length) == 'PNG'){
			var imgID = (img.id) ? "id='" + img.id + "' " : "";
			var imgClass = (img.className) ? "class='" + img.className + "' " : "";
			var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' ";
			var imgStyle = "display:inline-block;" + img.style.cssText;
			if (img.align == "left") imgStyle = "float:left;" + imgStyle;
			if (img.align == "right") imgStyle = "float:right;" + imgStyle;
			if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle;
			var strNewHTML = "<span " + imgID + imgClass + imgTitle
			+ " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
			+ "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
			+ "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>" 
			img.outerHTML = strNewHTML
			i = i-1;
		}
	}
}
//use if trying to use transparent pngs in IE
//window.attachEvent("onload", correctPNG);


function termsCheck(){
	if(this.document.terms.checkbox_terms.checked)
	{
		var checked = true;
	}else{
		var checked = false;
	}
	
	var xmlhttp=false;
	try {
           xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {
                xmlhttp = false;
            }
        }
	
	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {

		try {
			xmlhttp = new XMLHttpRequest();
		} catch (e) {
			xmlhttp=false;
		}
	}
	if (!xmlhttp && window.createRequest) {
		try {
			xmlhttp = window.createRequest();
		} catch (e) {
			xmlhttp=false;
		}
	}
 xmlhttp.open("GET", '/includes/Functions/identifier_ajax.php?checked='+checked, true);
 xmlhttp.onreadystatechange=handleResponse;
 xmlhttp.send(null);
//	var response = xmlhttp.responseText;
function handleResponse(){
	var response = xmlhttp.responseText;
	if(xmlhttp.readyState == 4){
		document.getElementById('show_form').innerHTML = response;
	}
	//else{document.getElementById('field_types').innerHTML = 'FAILED';
	//}
}
}


function loadIndentifiers(){
		var xmlhttp=false;
	try {
           xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {
                xmlhttp = false;
            }
        }
	
	if (!xmlhttp && typeof XMLHttpRequest!='undefined') {

		try {
			xmlhttp = new XMLHttpRequest();
		} catch (e) {
			xmlhttp=false;
		}
	}
	if (!xmlhttp && window.createRequest) {
		try {
			xmlhttp = window.createRequest();
		} catch (e) {
			xmlhttp=false;
		}
	}

 xmlhttp.open("GET", '/includes/Functions/identifier_ajax.php?id='+document.getElementById('identifier')
																											.value, true);
 xmlhttp.onreadystatechange=handleResponse;
 xmlhttp.send(null);
//	var response = xmlhttp.responseText;
function handleResponse(){
	var response = xmlhttp.responseText;
	if(xmlhttp.readyState == 4){
		document.getElementById('identifier_form').innerHTML = response;
	}
	//else{document.getElementById('identifier_form').innerHTML = 'FAILED';
	//}
}
}
