var t4name = false;
var t4email = false;
var t4comp = false;
var t4phone = false;
var t4country = false;

function t4Error(R, ID) {
	document.getElementById(ID).innerHTML = '<img src="' + GPath + 'iconError.jpg"> ' + R;
}

function t4Correct(ID) {
	document.getElementById(ID).innerHTML = '<img src="' + GPath + 'iconCorrect.jpg">';
}

function subQuote() {
	if((t4name) && (t4email) && (t4comp) && (t4phone)) {
		document.getElementById('submitquotebutton').disabled = '';	
		ua = document.getElementById('T4Email').value;
		ub = document.getElementById('T4Name').value + "|" + document.getElementById('T4Company').value + "|" + document.getElementById('T4Phone').value + "|" + $('#T4Country').val();
		ajaxFunc('proc_T4Var');
	}else{
		document.getElementById('submitquotebutton').disabled = 'disabled';
	}
}

function valName(V) {
	var regex = /^[a-zA-Z ]+$/;
	if(regex.test(V)) {
		if(V.length < 3) {
			//Name too short
			t4Error('Too Short', 'T4NameIco');
			t4name = false;
		}else{
			t4Correct('T4NameIco');
			t4name  = true;
		}
	}else{
		//Bad characters
		t4Error('Invalid Characters', 'T4NameIco');
		t4name = false;
	}
	subQuote();
}

function valEmail(V) {
	var regex = /^[a-zA-Z0-9._-]+@([a-zA-Z0-9.-]+\.)+[a-zA-Z0-9.-]{2,4}$/;
	var roctest = 'rocelec.com';
	if((regex.test(V)) && (!V.match(roctest))) {
		t4Correct('T4EmailIco');
		t4email  = true;
	}else{
		t4Error('Invalid Email', 'T4EmailIco');
		t4email = false;	
	}
	subQuote();
}

function valComp(V) {
	var regex = /^[a-zA-Z0-9 \'\.]+$/;
	if(regex.test(V)) {
		if(V.length < 3) {
			//Name too short
			t4Error('Too Short', 'T4CompIco');
			t4comp = false;
		}else{
			t4Correct('T4CompIco');
			t4comp  = true;
		}
	}else{
		//Bad characters
		t4Error('Invalid Characters', 'T4CompIco');
		t4comp = false;
	}
	subQuote();
}

function valPhone(V) {
	var regex = /^[0-9\-]+$/;
	if(regex.test(V)) {
		t4Correct('T4PhoneIco');
		t4phone  = true;
	}else{
		t4Error('Invalid Characters', 'T4PhoneIco');
		t4phone = false;	
	}
	subQuote();
}

function valCountry(V) {
	if(V != '') {
		t4Correct('T4CountryIco');
		t4country = true;
	} else {
		t4Error('Select country', 'T4CountryIco');
		t4country = false;
	}
	subQuote();
}

/* ============================= Date Codes =========================================*/	
function openDateCodes(PN) {
	ua = PN;
	ajaxFunc('proc_DateCodes');
}

function showDateCodes(RSP) {
	if (RSP == 'error') {
		alert('There was a problem getting those date codes.');	
	}else{
		var mydiv = document.getElementById('dc_'+ua);
		mydiv.style.display = 'block';
		mydiv.innerHTML = RSP;
	}
}
	
function closeDateCodes(PN) {
	document.getElementById('dc_'+PN).style.display = 'none';	
	document.getElementById('dclink_'+PN).href = 'javascript:scDateCodes(\''+PN+'\');'
}

function scDateCodes(PN) {
	document.getElementById('dc_'+PN).style.display = 'block';
}

/* ============================= Refine Search ========================================= */
function refineSearch(mfr, qty) {
	var partNumber = document.getElementById('refSearch').value;
	partNumber = partNumber.replace("+", "%2B");
	partNumber = partNumber.replace("/", "{*}");
	if(partNumber == '') { partNumber = '[*]'; }
	window.location='/about/searchresults/' + partNumber + '/0/'+mfr+'/'+qty+'/';
}

/* ============================= Show Details =========================================*/
function showDetails(V, MFG) {
	ua = escape(V);
	ub = escape(MFG);
	ajaxFunc('proc_SearchDetails');
}
/* ============================= Quote Cart =========================================*/	
	function addToCart(QTY, PN) {
		ua = QTY;
		ub = PN
		ajaxFunc('proc_AddCart');
	}
	function emptyCart() {
		ajaxFunc('proc_EmptyCart');
	}
	function viewCart() {
		ajaxFunc('proc_ViewCart');
	}
	function deleteCart(ID) {
		ua = ID;
		ajaxFunc('proc_DeleteCart');
	}
	function updateCart(QTY, ID) {
		ua = QTY;
		ub = ID;
		ajaxFunc('proc_UpdateCart');
	}
