function check_empty(OBJ){
	if(OBJ.value == "" || OBJ.length == 0){
		OBJ.style.backgroundColor = "#ff8888";
		return false;
	} else {
		OBJ.style.backgroundColor = "";
		return true;
	}
}

function init_form(FORM){
	var element, eles = FORM.elements;
	for (var i=0, j=eles.length; i<j; i++) {
		element = eles[i];
		if(element.value != "" && element.length != 0){
			element.style.backgroundColor = "";
		}
	}
}

function check_fields(FORM){
	var element, eles = FORM.elements;
	var goQuote = true;
	for (var i=0, j=eles.length; i<j; i++) {
		element = eles[i];
		if(element.style.backgroundColor == "rgb(255, 136, 136)"){
			goQuote = false;
		}
	}
	if(!goQuote){
		alert('Please ensure all fields marked with an astrix(*) or red background are filled in.');
	} else {
	   var total = document.getElementById('total_quote').innerHTML;
		total = total.replace(/[$]/g,'');
		total = total.replace(/[,]/g,'');
		total = parseFloat(total);
		if(total > 2499 && total < 5000001){
			FORM.submit();
		} else {
      	show_warning();
		}
	}
}

function update_total() {
	var hard_cost = document.quote_info.hard_cost.value;
	hard_cost = hard_cost.replace(/[$]/g,'');
	hard_cost = hard_cost.replace(/[,]/g,'');
	if(hard_cost.indexOf(".") != -1){
		if(hard_cost.length - hard_cost.indexOf(".") == 1) { hard_cost += "00"; }
		if(hard_cost.length - hard_cost.indexOf(".") == 2) { hard_cost += "0"; }
		if(hard_cost.length - hard_cost.indexOf(".") > 3) {
			var temp = hard_cost.indexOf(".") +2;
			hard_cost = hard_cost.substr(0, temp);
		}
		hard_cost = hard_cost.replace('.','');
	} else {
		hard_cost += "00";
	}

	var soft_cost = document.quote_info.soft_cost.value;
	soft_cost = soft_cost.replace(/[$]/g,'');
	soft_cost = soft_cost.replace(/[,]/g,'');
	if(soft_cost.indexOf(".") != -1){
		if(soft_cost.length - soft_cost.indexOf(".") == 1) { soft_cost += "00"; }
		if(soft_cost.length - soft_cost.indexOf(".") == 2) { soft_cost += "0"; }
		if(soft_cost.length - soft_cost.indexOf(".") > 3) {
			var temp = soft_cost.indexOf(".") +2;
			soft_cost = soft_cost.substr(0, temp);
		}
		soft_cost = soft_cost.replace('.','');
	} else {
		soft_cost += "00";
	}
	var total = (hard_cost*1) + (soft_cost*1);
	total = total + "";
	var temp = total.length - 2;
	total = total.substr(0, temp) + "." + total.substr(temp);
	document.getElementById('total_quote').innerHTML = currency_convert(total);
}

function currency_convert(text,dec) {
	if (null != text) {
		var temp = '$' + RComma(text.replace('$',''));  // add $ and commas
		return temp;
	}
}

function RComma(S) {
	S = String(S);
	var RgX = /^(.* )?([-+\u00A3\u20AC]?\d+)(\d{3}\b)/;
	return S == (S=S.replace(RgX, '$1$2,$3')) ? S : RComma(S);
}

function clear_vendor() {
	document.quote_info.contact_id.value = "0";
	document.quote_info.vendor_name.value = "";
	document.quote_info.vendor_title.value = "";
	document.quote_info.vendor_company.value = "";
	document.quote_info.vendor_addy1.value = "";
	document.quote_info.vendor_addy2.value = "";
	document.quote_info.vendor_addy3.value = "";
	document.quote_info.vendor_phone.value = "";
	document.quote_info.vendor_alt.value = "";
	document.quote_info.vendor_email.value = "";
	document.quote_info.comments.value = "";
}

function change_contact(){
	var layer = document.quote_info.contacts.selectedIndex - 1;
	document.quote_info.contact_id.value = contacts_array[layer][0];
	document.quote_info.vendor_name.value = contacts_array[layer][1];
   document.quote_info.vendor_title.value = contacts_array[layer][2];
	document.quote_info.vendor_company.value = contacts_array[layer][3];
	document.quote_info.vendor_addy1.value = contacts_array[layer][4];
	document.quote_info.vendor_addy2.value = contacts_array[layer][5];
	document.quote_info.vendor_addy3.value = contacts_array[layer][6];
	document.quote_info.vendor_phone.value = contacts_array[layer][7];
	document.quote_info.vendor_alt.value = contacts_array[layer][8];
	document.quote_info.vendor_email.value = contacts_array[layer][9];
	document.quote_info.comments.value = contacts_array[layer][10];
}

var email_count =0;
function email_addd(){
	var address = prompt("Please enter an e-mail address.", "");
	if (address!=null && address!=""){
		document.getElementById('email_list').innerHTML += ' <input type="text" id="emailCC_'+email_count+'" name="emailCC_'+email_count+'" value="'+address+'" onfocus="email_remove(this);" /> ';
		email_count++;
	}
}

function email_remove(ID){
	var ask = confirm("Remove this email address?")
	var remEl = ID;
	if ( remEl.parentNode && remEl.parentNode.removeChild && ask) {
		remEl.parentNode.removeChild(remEl);
	}
}
