function trimString(str) {
	str = this != window? this : str;
	return str.replace(/^\s+/g, '').replace(/\s+$/g, '');
}

function isEmpty(TmpString) {
	if (!TmpString) {
		return true;	
	}
	TmpString = trimString(TmpString);
	
	if (TmpString.length <= 0) {
		return true;
	}
	return false;
}

function convertToAscii(str) {
	var str = str.toLowerCase();
	var convert = "";
	for (var i=0; i<str.length; i++) {
		convert += str.charCodeAt(i);
	}
	return convert;
}

function showError(Errors) {
	Errors = Errors.toLowerCase();
	alert("The following error(s) occurred:\n" + Errors.substring(Errors,Errors.length-1) + "\n\nSorry can not Process the form");
	return false;
}

function addToSelect(selectBox, value, ID) {
	window.opener.addToOptionList(selectBox, value, ID);
	self.close();
}

function addToOptionList(selectBox, optionValue, optionText) {
	optionList = document.getElementById(selectBox);
	optionList[optionList.length] = new Option(optionValue, optionText);
}

function setCompany(Company) {
	document.getElementById("Company").value = Company;
}

function activeBannerBoxes() {
	bannerTitle = document.getElementById("BannerTitle");
	bannerURL = document.getElementById("URL");
	bannerIsImage = document.getElementById("BannerAsImage");
	bannerIsCode = document.getElementById("BannerAsCode");
	if (!isEmpty(document.getElementById("BannerType").value)) {
		bannerType = document.getElementById("BannerType").value;
		if (bannerType == "Flash") {
			bannerTitle.disabled = true;
			bannerURL.disabled = true;
			bannerIsImage.style.display = "block";
			bannerIsCode.style.display = "none";
		}	else if (bannerType == "Code") {
			bannerTitle.disabled = true;
			bannerURL.disabled = true;
			bannerIsImage.style.display = "none";
			bannerIsCode.style.display = "block";
		}	else {
			bannerTitle.disabled = false;
			bannerURL.disabled = false;
			bannerIsImage.style.display = "block";
			bannerIsCode.style.display = "none";
		}
	}
}

function activeVideoBoxes() {
	videoURL = document.getElementById("VideoURL");
	videoCode = document.getElementById("VideoText");
	if (!isEmpty(document.getElementById("VideoType").value)) {
		videoType = document.getElementById("VideoType").value;
		if (videoType == 1) {
			videoURL.disabled = true;
			videoCode.disabled = false;
			document.getElementById("VideoType1").style.display = "block";
			document.getElementById("VideoType2").style.display = "none";
		} else if (videoType == 2) {
			videoURL.disabled = false;
			videoCode.disabled = true;
			document.getElementById("VideoType1").style.display = "none";
			document.getElementById("VideoType2").style.display = "block";
		} else {
			document.getElementById("VideoType1").style.display = "none";
			document.getElementById("VideoType2").style.display = "none";
		}
	}
}

function formControls() {
	if (document.getElementById("registerForm")) {
		regFirstName = document.getElementById("registerForm").FirstName;
		regLastName = document.getElementById("registerForm").LastName;
		regUserName = document.getElementById("registerForm").UserName;
		regMobileNumber = document.getElementById("registerForm").MobileNumber;
		regEmailAddress = document.getElementById("registerForm").EmailAddress;
		regConfirmEmailAddress = document.getElementById("registerForm").ConfirmEmailAddress;
		regFirstName.onfocus = new Function("focusValues(regFirstName, 'First Name')");
		regFirstName.onblur = new Function("blurValues(regFirstName, 'First Name')");
		regLastName.onfocus = new Function("focusValues(regLastName, 'Last Name')");
		regLastName.onblur = new Function("blurValues(regLastName, 'Last Name')");
		regUserName.onfocus = new Function("focusValues(regUserName, 'Display Name')");
		regUserName.onblur = new Function("blurValues(regUserName, 'Display Name')");
		regMobileNumber.onfocus = new Function("focusValues(regMobileNumber, 'Mobile Number')");
		regMobileNumber.onblur = new Function("blurValues(regMobileNumber, 'Mobile Number')");
		regEmailAddress.onfocus = new Function("focusValues(regEmailAddress, 'Email Address')");
		regEmailAddress.onblur = new Function("blurValues(regEmailAddress, 'Email Address')");
		regConfirmEmailAddress.onfocus = new Function("focusValues(regConfirmEmailAddress, 'Confirm Email')");
		regConfirmEmailAddress.onblur = new Function("blurValues(regConfirmEmailAddress, 'Confirm Email')");
	} else if (document.getElementById("contactForm")) {
		conFirstName = document.getElementById("contactForm").FirstName;
		conLastName = document.getElementById("contactForm").LastName;
		conEmailAddress = document.getElementById("contactForm").EmailAddress;
		conComments = document.getElementById("contactForm").Comments;
		conFirstName.onfocus = new Function("focusValues(conFirstName, 'First Name')");
		conFirstName.onblur = new Function("blurValues(conFirstName, 'First Name')");
		conLastName.onfocus = new Function("focusValues(conLastName, 'Last Name')");
		conLastName.onblur = new Function("blurValues(conLastName, 'Last Name')");
		conEmailAddress.onfocus = new Function("focusValues(conEmailAddress, 'Email Address')");
		conEmailAddress.onblur = new Function("blurValues(conEmailAddress, 'Email Address')");
		conComments.onfocus = new Function("focusValues(conComments, 'Comments')");
		conComments.onblur = new Function("blurValues(conComments, 'Comments')");
	} else if (document.getElementById("advertisingForm")) {
		advFirstName = document.getElementById("advertisingForm").FirstName;
		advLastName = document.getElementById("advertisingForm").LastName;
		advEmailAddress = document.getElementById("advertisingForm").EmailAddress;
		advEnquiry = document.getElementById("advertisingForm").Enquiry;
		advFirstName.onfocus = new Function("focusValues(advFirstName, 'First Name')");
		advFirstName.onblur = new Function("blurValues(advFirstName, 'First Name')");
		advLastName.onfocus = new Function("focusValues(advLastName, 'Last Name')");
		advLastName.onblur = new Function("blurValues(advLastName, 'Last Name')");
		advEmailAddress.onfocus = new Function("focusValues(advEmailAddress, 'Email Address')");
		advEmailAddress.onblur = new Function("blurValues(advEmailAddress, 'Email Address')");
		advEnquiry.onfocus = new Function("focusValues(advEnquiry, 'Your Enquiry')");
		advEnquiry.onblur = new Function("blurValues(advEnquiry, 'Your Enquiry')");
	} else if (document.getElementById("tellFriendForm")) {
		tellYourName = document.getElementById("tellFriendForm").YourName;
		tellYourEmailAddress = document.getElementById("tellFriendForm").YourEmailAddress;
		tellFriendsName = document.getElementById("tellFriendForm").FriendsName;
		tellFriendsEmailAddress = document.getElementById("tellFriendForm").FriendsEmailAddress;
		tellMessage = document.getElementById("tellFriendForm").Message;
		tellYourName.onfocus = new Function("focusValues(tellYourName, 'Your Name')");
		tellYourName.onblur = new Function("blurValues(tellYourName, 'Your Name')");
		tellYourEmailAddress.onfocus= new Function("focusValues(tellYourEmailAddress, 'Your Email Address')");
		tellYourEmailAddress.onblur = new Function("blurValues(tellYourEmailAddress, 'Your Email Address')");
		tellFriendsName.onfocus = new Function("focusValues(tellFriendsName, 'Friends Name')");
		tellFriendsName.onblur = new Function("blurValues(tellFriendsName, 'Friends Name')");
		tellFriendsEmailAddress.onfocus= new Function("focusValues(tellFriendsEmailAddress, 'Friends Email Address')");
		tellFriendsEmailAddress.onblur= new Function("blurValues(tellFriendsEmailAddress, 'Friends Email Address')");
		tellMessage.onfocus = new Function("focusValues(tellMessage, 'Your Message')");
		tellMessage.onblur = new Function("blurValues(tellMessage, 'Your Message')");
	} else if ((document.getElementById("BannerCode")) && document.getElementById("BannerType")) {
		bannerType = document.getElementById("BannerType");
		bannerType.onchange = new Function("activeBannerBoxes()");
		activeBannerBoxes();
	} else if (document.getElementById("VideoType")) {
		videoType = document.getElementById("VideoType");
		videoType.onchange = new Function("activeVideoBoxes()");
		activeVideoBoxes();
	} else if (document.getElementById("progressiveCasinos")) {
		progressiveCasinoHolder = document.getElementById("progressiveCasinos");
		progressiveCasinoChecks = progressiveCasinoHolder.getElementsByTagName("input");
		for (var i=0;i<progressiveCasinoChecks.length; i++) {
			progressiveCasinoChecks[i].onclick = checkChecks;
		}
	}
	if (document.getElementById("topLoginForm")) {
		emailAddress = document.getElementById("topLoginForm").EmailAddress;
		emailAddress.onfocus = new Function("focusValues(emailAddress, 'Email')");
		emailAddress.onblur = new Function("blurValues(emailAddress, 'Email')");
		password = document.getElementById("topLoginForm").Password;
		password.onfocus = new Function("focusValues(password, 'Password')");
		password.onblur = new Function("blurValues(password, 'Password')");
	}
	if ((document.getElementById("CompanyID")) && (document.getElementById("CategoryID"))) {
		categoryBox = document.getElementById("CategoryID");
		categoryBox.onchange = new Function("populateSelect()");
		populateSelect();
	}
}

function focusValues(formElement, value) {
	if (formElement.value == value) {
		formElement.value = "";
	}
}

function blurValues(formElement, value) {
	if (formElement.value == "") {
		formElement.value = value;
	}
}

var maxChecks = 3;

function eventTrigger(e) {
	if (!e) {
		e = event;
	}
	return e.target || e.srcElement;
}

function checkChecks(e) {
	checkHolder = document.getElementById("progressiveCasinos");
	source = eventTrigger(e);
	var checkCount = 0;
	var checkError = false;
	if (checkHolder) {
		checkHolderChecks = checkHolder.getElementsByTagName("input");
		for (var i=0;i<checkHolderChecks.length; i++) {
			if (checkHolderChecks[i].checked) { 
				checkCount = checkCount + 1;
				if (checkCount > maxChecks) { 
					source.checked = false ;
					checkCount = checkCount-1 ;
					checkError = true;
				} else {
					checkError = false;
				}
			}
		}
		if (checkError) { 
			alert("you may only choose up to " + maxChecks + " options") ;
		}
	}
} 


function addTag() {
	currentValueHolder = document.getElementById("numTags");
	currentValue = currentValueHolder.value;
	nextValue = parseInt(currentValue) + 1;
	tagHolder = document.getElementById("Tags");
	newCol = tagHolder.appendChild(document.createElement("li"));
	newTagLabel = newCol.appendChild(document.createElement("label"));
	newTagLabel.htmlFor = "Tags" + nextValue;
	newTagLabel.innerHTML = "Tag " + nextValue;
	newTagInput = newCol.appendChild(document.createElement("input"));
	newTagInput.type = "text";
	newTagInput.name = "Tags[]";
	newTagInput.id = "Tags" + nextValue;
	currentValueHolder.value= nextValue;
}

function addProgressiveInfo() {
	currentValueHolder = document.getElementById("numProgressiveInfo");
	currentValue = currentValueHolder.value;
	nextValue = parseInt(currentValue) + 1;
	progressiveInfoHolder = document.getElementById("ProgressiveInfo");
	newCol = progressiveInfoHolder.appendChild(document.createElement("li"));
	newCol.className = "Hidden";
	newProgressiveInfoIDLabel = newCol.appendChild(document.createElement("label"));
	newProgressiveInfoIDLabel.innerHTML = "ProgressiveInfo " + nextValue + " ID";
	newProgressiveInfoIDInput = newCol.appendChild(document.createElement("input"));
	newProgressiveInfoIDInput.type = "text";
	newProgressiveInfoIDInput.name = "ProgressiveInfoID[]";
	newProgressiveInfoIDInput.id = "ProgressiveInfo" + nextValue + "ID";
	
	newCol = progressiveInfoHolder.appendChild(document.createElement("li"));
	newProgressiveInfoTitleLabel = newCol.appendChild(document.createElement("label"));
	newProgressiveInfoTitleLabel.innerHTML = "ProgressiveInfo " + nextValue + " Title";
	newProgressiveInfoTitleInput = newCol.appendChild(document.createElement("input"));
	newProgressiveInfoTitleInput.type = "text";
	newProgressiveInfoTitleInput.name = "ProgressiveInfoTitle[]";
	newProgressiveInfoTitleInput.id = "ProgressiveInfo" + nextValue + "Title";
	
	newCol = progressiveInfoHolder.appendChild(document.createElement("li"));
	newProgressiveInfoTextLabel = newCol.appendChild(document.createElement("label"));
	newProgressiveInfoTextLabel.innerHTML = "ProgressiveInfo " + nextValue + " Text";
	newProgressiveInfoTextInput = newCol.appendChild(document.createElement("input"));
	newProgressiveInfoTextInput.type = "text";
	newProgressiveInfoTextInput.name = "ProgressiveInfoText[]";
	newProgressiveInfoTextInput.id = "ProgressiveInfo" + nextValue + "Text";
	
	currentValueHolder.value= nextValue;
}

function clearOptions(obj) {
	if (obj) {
		obj.length = 0;
	}
	objGroups = obj.getElementsByTagName("optgroup");
	if (objGroups.length >= 1) {
		for (var i=objGroups.length-1;i>=0;i--) {
			obj.removeChild(objGroups[i]);
		}
	}
}
