function validateNotEmpty(field)
{
	if(document.getElementById(field).value=="" || (field == 'password' && document.getElementById(field).value.length < 6)) {
		document.getElementById(field).setAttribute("class", "input-error"); 
		if (document.getElementById(field+'-icon')!=null)
			document.getElementById(field+'-icon').setAttribute("class", "icon-error"); 
		return false;
	} else {
		document.getElementById(field).setAttribute("class", "input-check"); 
		if (document.getElementById(field+'-icon')!=null)
			document.getElementById(field+'-icon').setAttribute("class", "icon-check"); 
		return true;
	}
}

//Validate phone number for 10 digit US numbers.
//phoneField - The HTML input field containing the phone number to validate.
//format - Integer value that defines how to format the text field.
function validatePhone(phoneField) {
	var num = phoneField.value.replace(/[^\d]/g,'');

	if (num.length<4) {
		phoneField.value = num.substring(0,3);
	} else {
		if (num.length<7) {
			phoneField.value = "(" + num.substring(0,3) + ")"+num.substring(3, 6);
		} else {
			phoneField.value ="(" + num.substring(0,3) + ")"+num.substring(3, 6)+"-" + num.substring(6,10);
		}
	}
}

function validateNumber(numField) {
	numField.value = numField.value.replace(/[^\d]/g,'');
}	
function validateZip(zipField) {
	var num = zipField.value.replace(/[^\d]/g,'');
	zipField.value = num.substring(0,5);
}

function updateCityState(root) {
	if (!validateNotEmpty('zipcode')) return false;
	  
	if (!isWorking && http) {
		var url = root+"ajax/getcitystate.php?param="; // The server-side script
		var zipValue = document.getElementById("zipcode").value; 
	 	http.open("GET", url + escape(zipValue), true); 
	 	http.onreadystatechange = handleZipResponse; 
	 	isWorking = true;
	 	http.send(null);
	}
}

function handleZipResponse() {
  if (http.readyState == 4) {
        if ( document.getElementById('city') != undefined && document.getElementById('state') != undefined) {
	  if (http.responseText.indexOf('invalid') == -1) {
	        // Split the comma delimited response into an array
	        results = http.responseText.split(",");
	        document.getElementById('city').value = results[0];
	        validateNotEmpty('city');
	        document.getElementById('state').value = results[1];
	        validateNotEmpty('state');
	  } else {
	        document.getElementById('city').value = '';
	        validateNotEmpty('city');
	        document.getElementById('state').value = '';
    		validateNotEmpty('state');
	  }
        }
        isWorking = false;
  }
}

function getHTTPObject() { 
	var xmlhttp; 
	/*@cc_on @if (@_jscript_version >= 5) 
	try { 
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); 
	} catch (e) { 
		try { 
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
			} catch (E) { 
				xmlhttp = false; 
				} } 
	@else xmlhttp = false; 
	@end @*/ 
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') { 
		try { 
			xmlhttp = new XMLHttpRequest(); 
		} catch (e) { 
			xmlhttp = false;
		}
	} 
		return xmlhttp; 
} 

var isWorking = false;
var http = getHTTPObject(); // We create the HTTP Object

var W3CDOM = (document.createElement && document.getElementsByTagName);

function initFileUploads(inputclass) {
	if (!W3CDOM) return;
	var fakeFileUpload = document.createElement('div');
	fakeFileUpload.className = 'fakefile';
	var inputtext= document.createElement('input');
	inputtext.type = 'text';
	inputtext.className = inputclass;
	fakeFileUpload.appendChild(inputtext);
	var image = document.createElement('div');
	image.className='icon-folder';
	fakeFileUpload.appendChild(image);
	var label = document.createElement('div');
	label.className='file-label';
	label.innerHTML='Choose File';
	fakeFileUpload.appendChild(label);
	var x = document.getElementsByTagName('input');
	for (var i=0;i<x.length;i++) {
		if (x[i].type != 'file') continue;
		if (x[i].parentNode.className != 'fileinputs') continue;
		x[i].className = 'file hidden';
		var clone = fakeFileUpload.cloneNode(true);
		x[i].parentNode.appendChild(clone);
		x[i].relatedElement = clone.getElementsByTagName('input')[0];
		x[i].onchange = x[i].onmouseout = function () {
			this.relatedElement.value = this.value;
		}
	}
}

function array_contains( needle, haystack ) {
   for (i in haystack) {
       if (haystack[i] == needle) return true;
   }
   return false;
}

