// .nugthat JS

/********************
Product download page control routine:
********************/
function initDownloadPage (selectedItems, preLink) {
	ticked = parseInt(1 + selectedItems);
	prevField = preLink;
	boxTicked = false;
}
requiredFields = new Array('playerPRO', 'compiler', 'tellWorld', 'keepUpdated');
function toggle (theField, emailReq) {
	foundOne = false;
	boxTicked = document.options[theField].checked;
	if (boxTicked) {
		document.getElementById(theField + "_tr").style.backgroundColor = '#F0F0F0';
	} else {
		document.getElementById(theField + "_tr").style.backgroundColor = '#FFFFFF';
	}
	for (i = 0; i < requiredFields.length; i++) {
		if (document.options[requiredFields[i]].checked) {
			foundOne = true;
		} 
		foundOne == true ? document.options['emailFlag'].value = 'true' : document.options['emailFlag'].value = 'false';
	}
}
/********************
Checks status of the email address box - if anything is inputted it gets rid of the nag text
********************/
function changeStatus (theStatus) {
	if (theStatus == 'oops' && notRequired == false) {
		document.getElementById('emailRequired').innerHTML = "Email Address Required";
		emailOK = false;
	} else {
		document.getElementById('emailRequired').innerHTML = "";
		emailOK = true;
	}
}
/********************
Jump menu for skins page and nugs download page
********************/
function jumpMenu (targ, selObj, restore){
	var param = selObj.options[selObj.selectedIndex].value;
	if (param != 'Select') {
		eval (targ + ".location='index.php?sortOn=" + param + "'");
		if (restore) selObj.selectedIndex = 0;
	}
}
/********************
start download after product has been chosen
********************/
function beginDownload () {
  bin = "../../download/.nugthat Player " + prodVer + ".exe";
  document.location = bin;
  clearTimeout(dl);
}
/********************
init a timed execution of beginDownload, cancel the call straight afterwards to stop it being offered twice
********************/
function initDL (version) {
   prodVer = version.toString();
   dl = setTimeout ("beginDownload()", '5000');
   c = setTimeout('clearTimeout(dl)', '5050');
}
/********************
field validation for tellYourFriends
********************/
var reg_email_address_string = /^(.+@[^\.].*\.[a-z]{2,})(;.+@[^\.].*\.[a-z]{2,})*$/
function isEmailAddressString(strEmailAddressString) {
    return isValid(strEmailAddressString, reg_email_address_string);
}
function isValid(strValue, regExp) {
	// set the default return value
	var blnResult = true;
	var arrMatch = strValue.match(regExp);
	if (arrMatch == null) {
		blnResult = false;
	}
	return (blnResult);
}
function validateTellFriends () {
	if (isEmailAddressString(document.tellAFriend.email.value) == false) {
		document.getElementById('error').innerHTML = "Please make sure your email address is formatted properly";
		return false;
	} else if (isEmailAddressString(document.tellAFriend.addresses.value) == false) {
		document.getElementById('error').innerHTML = "Please make sure you filled in your friends' email addresses correctly separated by \";\"";
		return false;
	}
}
/********************
field validation for contact form
********************/
function validateContact() {
	var allowed = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (document.contact.regarding.value == "" || document.contact.regarding.value == "Please Choose...") {
		//alert ("Please complete the Regarding field");
		document.getElementById('error').innerHTML = "Please make sure you selected a subject";
		return false;
	} else if (allowed.test (document.contact.email.value) == false) {
		//alert ("Please complete the Email field");
		document.getElementById('error').innerHTML = "Please make sure your email address is formatted properly";
		return false;
	} else if (document.contact.message.value == "") {
		//alert ("Please complete Enquiry Field");
		document.getElementById('error').innerHTML = "Please make sure you filled out the enquiry field";
		return false;
	}
}
/*******************
field validation for uninstall form
*******************/
function validateUninstall() {
	var allowed = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if ((document.uninstallForm.emailAddress.value != "") && (allowed.test (document.uninstallForm.emailAddress.value) == false)) {
		document.getElementById('error').innerHTML = "Please make sure your email address is formatted properly";
		return false;
	}
}
/*******************
star swap loop for ratings / star click event for voting procedure / star browser status message
*******************/
function swapIMG (theStar, state) {
	rating = theStar.substr(theStar.length - 1);
	idPrefix = theStar.substr(0, 1);
	if (state == 'on') {
		for (var i = 1; i <=rating; i++) {
			document.getElementById(idPrefix+'_star'+i).src = '../images/ratings/starOn.gif';
		}
		for (i = i; i <=5; i++) {
			document.getElementById(idPrefix+'_star'+i).src = '../images/ratings/starOff.gif';
		}
	}
}
function swapStatus (state) {
		if (state == 'on') {
			window.status = "Give this .nug a rating!";
			return true;
		} else {
			window.status = "";
			return true;
		}
}
/*******************
none-skinned nugs <td> expander function
*******************/
function expandNugTable (theSpan, fullText) {
	if (document.getElementById('nugTable_'+theSpan).style.display == 'none') {
		document.getElementById('nugTable_'+theSpan).style.display = '';
		document.getElementById('holder_nugTable_'+theSpan).innerHTML = fullText;
		document.getElementById('expand'+theSpan).src = '../images/expanded.gif';
	} else {
		document.getElementById('nugTable_'+theSpan).style.display = 'none';
		document.getElementById('expand'+theSpan).src = '../images/expand.gif';
		fullText.length > 100 ? document.getElementById('holder_nugTable_'+theSpan).innerHTML = fullText.substr(0, 100) + '...' : document.getElementById('holder_nugTable_'+theSpan).innerHTML = fullText;
	}
	return false;
}


