/**
 * @author alexfloroiu
 */
var onVerifyForm = false;;
var errors = 0;

var btnSubmitID = 'btnSubmit';
function manager(notError, field) {
	var label = document.getElementById('lbl' + field.name);
	var colorLabel = label.style.color;
	if(onVerifyForm && onVerifyForm == true)return;
	if (notError) {
		if (colorLabel == colorError)
			errors--; 
		field.valid=true;	
		label.style.color = colorValid;
		//if (errors == 0)
		//	document.getElementById(btnSubmitID).disabled = false;
	} else {
		if (colorLabel == colorValid)
			errors++; 
		field.valid=false;
		label.style.color = colorError;
		//document.getElementById(btnSubmitID).disabled = true;
	}

}

function CapitalizeNames(field) {
	var ValueString = new String();
	eval('ValueString=field.value');

	ValueString = ValueString.replace(/ +/g, ' ');
	var names = ValueString.split(' ');
	for (var i = 0; i < names.length; i++) {
		if (names[i].length > 1) {
			names[i] = names[i].toLowerCase();
			letters = names[i].split('');
			letters[0] = letters[0].toUpperCase();
			names[i] = letters.join('');
		} else {
			names[i] = names[i].toUpperCase();
		}
	}
	ValueString = names.join(' ');

	eval('field.value=ValueString');
	return true;
}

function isMatch(elem, patern) {

	if (elem.value.match(patern)) {
		return true;
	} else {
		elem.focus();
		return false;
	}
}
function checkRequiredValues(field, label) {
	CapitalizeNames(field);
	var alphaExp = /^[a-zA-Z\s,\.\-]+$/;
	manager((field.value.length > 1) && (isMatch(field, alphaExp)), field,
			label);
}
function checkEmail(field, label) {
	var emailExp = /^[\w\-\.\+]+\@[a-zA-Z0-9\.\-]+\.[a-zA-z0-9]{2,4}$/;
	manager(
			((field.value.length > 1) && (isMatch(field, emailExp)) || (field.value.length == 0)),
			field, label);
}

function checkZip(field, label) {
	var numericExpression = /^[0-9]{5}([\+]{1}[0-9]{4}){0,1}$/;
	manager(
			((field.value.length > 1) && (isMatch(field, numericExpression)) || (field.value.length == 0)),
			field, label);
}
function checkLetters(field, label) {

	field.value = field.value.toUpperCase();
	var alphaExp = /^[a-zA-Z]+$/;
	manager(
			((field.value.length > 1) && (isMatch(field, alphaExp)) || (field.value.length == 0)),
			field, label);
}
function checkTitle(field, label) {
	CapitalizeNames(field);
	var alphaExp = /^[a-zA-Z/'\s,\.]+$/;
	manager(
			((field.value.length > 1) && (isMatch(field, alphaExp)) || (field.value.length == 0)),
			field, label);
}
function checkAddress(field, label) {
	var alphaExp = /^[0-9a-zA-Z\s/'#\.,-]+$/;
	manager(
			((field.value.length > 1) && (isMatch(field, alphaExp)) || (field.value.length == 0)),
			field, label);
}

function checkCompany(field, label) {
	var alphaExp = /^[0-9a-zA-Z\s/'!\.]+$/;
	manager(
			((field.value.length > 1) && (isMatch(field, alphaExp)) || (field.value.length == 0)),
			field, label);
}

function crossCheck(inTag, otherFieldID) {
	if (!document.getElementById(otherFieldID)) {
		return false;
	}
	return (inTag.value == document.getElementById(otherFieldID).value);
}

function checkboxPicked(checkboxName) {
	var checkboxSet = "";

	for (var k = 0; k < document.forms.length; k++) {
		if (!checkboxSet) {
			checkboxSet = document.forms[k][checkboxName];
		}
	}
	if (!checkboxSet)
		return false;
	for (k = 0; k < checkboxSet.length; k++) {
		if (checkboxSet[k].checked) {
			return true;
		}
	}
	return false;
}


function radioValue(radioName) {
	var radioSet = "";

	for (var k = 0; k < document.forms.length; k++) {
		if (!radioSet) {
			radioSet = document.forms[k][radioName];
		}
	}
	if (!radioSet)
		return "";
	for (k = 0; k < radioSet.length; k++) {
		if (radioSet[k].checked) {
			return radioSet[k].value;
		}
	}
	return "";
}

function getFormsElement(theName){
	for (var k = 0; k < document.forms.length; k++) {
		if (document.forms[k][theName]) {
			return document.forms[k][theName];
		}
	}
}

function radioPicked(radioName) {
	var radioSet = "";

	for (var k = 0; k < document.forms.length; k++) {
		if (!radioSet) {
			radioSet = document.forms[k][radioName];
		}
	}
	if (!radioSet)
		return false;
	for (k = 0; k < radioSet.length; k++) {
		if (radioSet[k].checked) {
			return true;
		}
	}
	return false;
}

function fieldLength(field, label, length) {
	if (!length)
		length = 0;
	if (field.value.length == 0) {// field.value=="" ||
		return false;
		label.style.color = '#FF0000';
	}
	return true;
}

// usage: onclick="javascript:getIt(this)"
var txtPhoneGlobal;
function ValidatePhone(txtphone) {

	if (!txtphone)
		txtphone = txtPhoneGlobal;

	p = txtPhoneGlobal.value;
	if (p.length == 3) {
		// d10=p.indexOf('(')
		pp = p;
		d4 = p.indexOf('(')
		d5 = p.indexOf(')')
		if (d4 == -1) {
			pp = "(" + pp;
		}
		if (d5 == -1) {
			pp = pp + ")";
		}
		// pp="("+pp+")";
		txtphone.value = "";
		txtphone.value = pp;
	}
	if (p.length > 3) {
		d1 = p.indexOf('(')
		d2 = p.indexOf(')')
		if (d2 == -1) {
			l30 = p.length;
			p30 = p.substring(0, 4);
			// alert(p30);
			p30 = p30 + ")"
			p31 = p.substring(4, l30);
			pp = p30 + p31;
			// alert(p31);
			txtphone.value = "";
			txtphone.value = pp;
		}
	}
	if (p.length > 5) {
		p11 = p.substring(d1 + 1, d2);
		if (p11.length > 3) {
			p12 = p11;
			l12 = p12.length;
			l15 = p.length
			// l12=l12-3
			p13 = p11.substring(0, 3);
			p14 = p11.substring(3, l12);
			p15 = p.substring(d2 + 1, l15);
			txtphone.value = "";
			pp = "(" + p13 + ")" + p14 + p15;
			txtphone.value = pp;
			// obj1.value="";
			// obj1.value=pp;
		}
		l16 = p.length;
		p16 = p.substring(d2 + 1, l16);
		l17 = p16.length;
		if (l17 > 3 && p16.indexOf('-') == -1) {
			p17 = p.substring(d2 + 1, d2 + 4);
			p18 = p.substring(d2 + 4, l16);
			p19 = p.substring(0, d2 + 1);
			// alert(p19);
			pp = p19 + p17 + "-" + p18;
			txtphone.value = "";
			txtphone.value = pp;
			// obj1.value="";
			// obj1.value=pp;
		}
	}
	// }
	setTimeout(ValidatePhone, 100)
}
function getIt(m) {
	txtPhoneGlobal = m;
	ValidatePhone(m);
}

// usage onKeyDown="validateZip(this , event);"
function validateZip(txtzip, e) {
	var code = getKeyCodePressed(e);
	if (code != 8) {// && code!= 37 && code!=38
		var poz = txtzip.value.indexOf('+');
		if (poz == -1) {
			if (txtzip.value.length == 5)
				txtzip.value = txtzip.value + "+";
		} else {

			if (txtzip.value.length > 5) {
				l = txtzip.value.length;
				s04 = txtzip.value.substring(0, 5);
				// alert(p30);
				s04 = s04 + "+"
				s4l = txtzip.value.substring(6, l);
				pp = s04 + s4l;
				// alert(p31);
				txtzip.value = "";
				txtzip.value = pp;
			} else if (txtzip.value.length < 5) {
				l = txtzip.value.length;
				s04 = txtzip.value.replace(/\+/, "");
				// alert(p30);
				s04 = s04 + "+";
				// alert(p31);
				txtzip.value = "";
				txtzip.value = s04;
			}

		}

	}
}

// function testphone(obj1) {
// p = obj1.value
// // alert(p)
// p = p.replace("(", "")
// p = p.replace(")", "")
// p = p.replace("-", "")
// p = p.replace("-", "")
// // alert(isNaN(p))
// if (isNaN(p) == true) {
// alert("Check phone");
// return false;
// }
// }

//
// function verifyForm() {
// // alert("test");
// document.getElementById('label_first_name').style.color = '#000';
// document.getElementById('label_last_name').style.color = '#000';
// document.getElementById('label_email').style.color = '#000';
// document.getElementById('error_message').style.display = "none";
//
// var themessage = false;
// if (document.getElementById('first_name').value == "") {
// themessage = true;
// document.getElementById('label_first_name').style.color = '#FF0000';
// }
//
// if (document.getElementById('last_name').value == "") {
// themessage = true;
// document.getElementById('label_last_name').style.color = '#FF0000';
// }
// if (document.getElementById('email').value == "") {
// themessage = true;
// document.getElementById('label_email').style.color = '#FF0000';
// }
// // alert if fields are empty and cancel form submit
// if (!themessage) {
// document.getElementsByTagName('Form')[0].action =
// 'https://www.salesforce.com/servlet/servlet.WebToLead?encoding=UTF-8';
// document.getElementsByTagName('Form')[0].submit();
// } else {
// document.getElementById('error_message').style.display = "inline";
//
// return false;
// }
// }

// var initRequestPath = "ValidateContact.php";
// var requestPath;
// var theID;
// var task;
// function checkField(field) {
// // alert('xxx');
// task = "validate";
// theID = 'lbl' + field.id;
// requestPath = initRequestPath + "?id=" + field.id + "&v=" + field.value;
// updateField();
//
// //
// // var label = document.getElementById('lbl'+field.id);
// // switch(field.id){
// // case "FirstName":
// // return fieldLength(field,label);
// // break;
// // case "LastName":
// // return fieldLength(field,label);
// // break;
// // case "Email":
// // return checkEmail(field,label);
// // break;
// //
// // }
//
// }
