/*******************************************************************************
 * Download CSSoftware
 ******************************************************************************/
function checkAll(FormName, FieldName, CheckValue) {
	if (!document.forms[FormName])
		return;
	var objCheckBoxes = document.forms[FormName].elements[FieldName];
	if (!objCheckBoxes)
		return;
	var countCheckBoxes = objCheckBoxes.length;
	if (!countCheckBoxes)
		objCheckBoxes.checked = CheckValue;
	else
		// set the check value for all check boxes
		for ( var i = 0; i < countCheckBoxes; i++)
			objCheckBoxes[i].checked = CheckValue;

}

function validateCS(frm,check)
{
	var objCheckBoxes = document.forms[frm].elements[check];
	var countCheckBoxes = objCheckBoxes.length;
	c =0;
	for ( var i = 0; i < countCheckBoxes; i++)
	{
		 if(objCheckBoxes[i].checked==true)
	     {
	    	 c=c+1
	     }
	}
	if(c==0)
	{
		alert("Please select a checkbox or click download link");
		return false;
	}
	return true;	
	
}

function popup(url, w, h) {

	var width = w;
	var height = h;
	var left = (screen.width - width) / 2;
	var top = (screen.height - height) / 2;
	var params = 'width=' + width + ', height=' + height;
	params += ', top=' + top + ', left=' + left;
	params += ', directories=no';
	params += ', location=no';
	params += ', menubar=no';
	params += ', resizable=no';
	params += ', scrollbars=yes';
	params += ', status=yes';
	params += ', toolbar=yes';
	newwin = window.open(url, 'windowname5', params);
	// newwin.focus();
	return false;
}

/*******************************************************************************
 * EmailtoCustomer [ downloadlink]
 ******************************************************************************/
function EmailtoCustomer(id, linkid, email) {
	popup("emaildownloadlink.php?id=" + id + "&linkid=" + linkid + "&email="
			+ email, 300, 200);
}

/*******************************************************************************
 * Validation for Sales/Demo Form
 ******************************************************************************/

function validateSales(f) {
	var reason = "";
	reason += validateEmpty(f.fname, "First Name");
	reason += validateEmail(f.email);
	reason += validatePhone(f.phone);
	reason += validateTextArea(document.getElementById("comment").name,
			document.getElementById("comment").value);// field name, val
	if (reason != "") {
		alert("Some fields need correction:\n" + reason);
		return false;
	}
	return true;
}

/*******************************************************************************
 * Validation for RMA Form
 ******************************************************************************/

function validateRMA(f) {
	var reason = "";
	reason += validateEmpty(f.hospital, "Hospital");
	samecustomer = document.getElementById("samecustomer");
	if (!samecustomer.checked)
		reason += validateEmpty(f.enduser, "End User");

	reason += validatePhone(f.phone);
	reason += validateEmail(f.email);
	reason += validateTextArea(document.getElementById("modelname").name,
			document.getElementById("modelname").value);
	reason += validateTextArea(document.getElementById("serialno").name,
			document.getElementById("serialno").value);
	reason += validateTextArea(document.getElementById("problem").name,
			document.getElementById("problem").value);

	if (reason != "") {
		alert("Some fields need correction:\n" + reason);
		return false;
	}
	return true;

}

/*******************************************************************************
 * Download Calibration Software
 ******************************************************************************/
function validateDownloadSoftware(f) {
	var reason = "";
	reason += validateEmpty(f.company, "Company");
	reason += validateEmpty(f.contactperson, "Contact Person");
	reason += validatePhone(f.phone);
	reason += validateEmail(f.email);
	if (reason != "") {
		alert("Some fields need correction:\n" + reason);
		return false;
	}
	return true;
}

/*******************************************************************************
 * Admin
 ******************************************************************************/
function validateAdmin(f) {
	var reason = "";
	reason += validateEmpty(f.login, "Login");
	reason += validateEmpty(f.password, "Password");
	if (reason != "") {
		alert("Some fields need correction:\n" + reason);
		return false;
	}
	return true;
}

/*******************************************************************************
 * General Validation Functions
 ******************************************************************************/

function trim(s) {
	return s.replace(/^\s+|\s+$/, '');
}

/*******************************************************************************
 * Validation for TextArea [fldname, fldvalue]
 ******************************************************************************/

function validateTextArea(fld, txtval) {
	txt = trim(txtval);
	var error = "";
	if (txt == "") {
		document.getElementById(fld).style.background = '#faeeb8';
		error = "You did not enter " + fld + "\n";
	} else {
		document.getElementById(fld).style.background = 'White';
	}
	return error;
}

/*******************************************************************************
 * Validation for Empty Field[fldname,fldvalue]
 ******************************************************************************/

// fldname, fldvalue
function validateEmpty(fld, fldname) {
	var error = "";
	if (fld.value.length == 0) {
		fld.style.background = '#faeeb8';
		error = "You did not enter " + fldname + "\n";
	} else {
		fld.style.background = 'White';
	}
	return error;
}

/*******************************************************************************
 * Validation for UserName
 ******************************************************************************/

function validateUsername(fld) {
	var error = "";
	var illegalChars = /\W/; // allow letters, numbers, and underscores

	if (fld.value == "") {
		fld.style.background = '#faeeb8';
		error = "You didn't enter a username.\n";
	} else if ((fld.value.length < 5) || (fld.value.length > 15)) {
		fld.style.background = '#faeeb8';
		error = "The username is the wrong length.\n";
	} else if (illegalChars.test(fld.value)) {
		fld.style.background = '#faeeb8';
		error = "The username contains illegal characters.\n";
	} else {
		fld.style.background = 'White';
	}
	return error;
}
/*******************************************************************************
 * Validation for Password
 ******************************************************************************/

function validatePassword(fld) {
	var error = "";
	var illegalChars = /[\W_]/; // allow only letters and numbers

	if (fld.value == "") {
		fld.style.background = '#faeeb8';
		error = "You didn't enter a password.\n";
	} else if ((fld.value.length < 7) || (fld.value.length > 15)) {
		error = "The password is the wrong length. \n";
		fld.style.background = '#faeeb8';
	} else if (illegalChars.test(fld.value)) {
		error = "The password contains illegal characters.\n";
		fld.style.background = '#faeeb8';
	} else if (!((fld.value.search(/(a-z)+/)) && (fld.value.search(/(0-9)+/)))) {
		error = "The password must contain at least one numeral.\n";
		fld.style.background = '#faeeb8';
	} else {
		fld.style.background = 'White';
	}
	return error;
}

/*******************************************************************************
 * Validation for Email
 ******************************************************************************/

function validateEmail(fld) {
	var error = "";
	var tfld = trim(fld.value); // value of field with whitespace trimmed off
	var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/;
	var illegalChars = /[\(\)\<\>\,\;\:\\\"\[\]]/;

	if (fld.value == "") {
		fld.style.background = '#faeeb8';
		error = "You didn't enter an email address.\n";
	} else if (!emailFilter.test(tfld)) { // test email for illegal characters
		fld.style.background = '#faeeb8';
		error = "Please enter a valid email address.\n";
	} else if (fld.value.match(illegalChars)) {
		fld.style.background = '#faeeb8';
		error = "The email address contains illegal characters.\n";
	} else {
		fld.style.background = 'White';
	}
	return error;
}

/*******************************************************************************
 * Validation for Phone
 ******************************************************************************/

function validatePhone(fld) {
	var error = "";
	var tfld = trim(fld.value);
	var phoneFilter = /^[2-9]\d{2}-\d{3}-\d{4}$/;
	if (tfld.length == 0) {
		error = "You didn't enter a phone number.\n";
		fld.style.background = '#faeeb8';
	} else if (!phoneFilter.test(tfld)) {
		error = "Enter correct phone(Ex:952-899-0000)\n";
		fld.style.background = '#faeeb8';
	} else {
		fld.style.background = 'White';
	}
	return error;
}

/*******************************************************************************
 * Get Radio Button Value
 ******************************************************************************/

function get_radio_value(fld) {
	for ( var i = 0; i < fld.length; i++) {
		if (fld[i].checked) {
			var rad_val = fld[i].value;
		}
	}
}

/*******************************************************************************
 * CheckBox Values
 ******************************************************************************/

function get_check_value(fld) {
	var c_value = "";
	for ( var i = 0; i < fld.length; i++) {
		if (fld[i].checked) {
			c_value = c_value + fld[i].value + "\n";
		}
	}
}

/*******************************************************************************
 * if Checkbox status is true, make a textfield as readonly
 ******************************************************************************/
function makeReadOnly(txt) {
	document.getElementById(txt).readOnly = "true";
	document.getElementById(txt).style.background = 'White'

}

function createurl(frm,check)
{	
	var objCheckBoxes = document.forms[frm].elements[check];
	var countCheckBoxes = objCheckBoxes.length;
	c =0;
	str = 'downloadcssoftware.php?dispform=true&sendrequest=SEND+REQUEST';
	for ( var i = 0; i < countCheckBoxes; i++)
	{
		 if(objCheckBoxes[i].checked==true)
	     {	    	 
	    	 str = str + '&choice[]='+objCheckBoxes[i].value;
	     }
	}		
	res = validateCS(frm,check);
	if(res){		
	 tb_show('SEND REQUEST',str,'');
	}
	
}

