/*
Amuletten Home Site
Auther: David Coomber
jscripts.js, 060323 Revised: 060323
Contains JavaScript Functions
*/

/* General Script Functions (First Section):

txtCnt(Form&fieldId, counterfieldId, maxChar allowed)
isDate(date)
oneWordOnly(string)
getRadio_buttons(radio_group_name)
checkEmailadrs(address1,address2)
checkCleantext(text)

*/

// Function: txtCnt(FormId.fieldId, FormId.counterfieldId, maxChar allowed). 
// 																														Checks to see max. number of chars not exceeded.
// Arguments: Form ID.Field ID to be checked, Form ID.Feild ID of Counter, Max. number or characters allowed.
// Returns: none.
// Notes: Turns field pink when extra character added as warning. Should be used with 'onFocus() & onKeyDown()'
// in field tags. Counter can be added as text field 'in form' as extra warning:
//							Ex. <input readonly type=text name=remLen size=3 maxlength=3 value=''>&nbsp;&nbsp;Tecken kvar

function txtCnt(field, countfield, maxlimit) {
field.style.backgroundColor="white";
if (field.value.length >= maxlimit) {							// if too long, trim!
field.value = field.value.substring(0, maxlimit);  //Change background colour as warning
field.style.backgroundColor="pink";
}
else																					// otherwise, update 'characters left' counter
countfield.value = maxlimit - field.value.length;
}


// Function: isDate(Date String). Checks to see date is in yy-mm-dd format.
// Arguments: Date String.
// Returns: True=Format OK, False=Wrong Format.

function isDate(datestr) {
var pattern = /^(\d{2})(\-)(\d{2})(\-)(\d{2})$/;
var arrayMatch = datestr.match(pattern);
	if (arrayMatch == null) {
	return false;
	}
}
// End of Function

// Function: oneWordOnly(String). Checks to see if String contains more than one word.
// Arguments: String.
// Returns: True=Only One Word, False=More than One Word.
function oneWordOnly(str) {
var cntWords=str.value.split(" ");
	if (cntWords.length > 1) return false;
return true;
}
// End of Function

// Function: getRadio_button(radio_group_name). Checks which Radio Button is Selected.
// Arguments: Name of Radio group.
// Returns: True=Radio Button selected, False=No selection, error.

function getRadioButton(radioGroupName) {
var radioGet = document.getElementsByName(radioGroupName);

for(i = 0;i < radioGet.length; i++) {
		if (radioGet[i].checked)
		return (radioGet[i].value);
}
return false;
}
// End of Function


// Function: checkEmailadrs(address1,address2). Simple E-Mail Check with confimation.
// Arguments: First e-mail address to be checked, Second e-mail address to be compared against first.
// Returns: True=OK, False=Error (with text prompt).

function checkEmailadrs (eaddress1, eaddress2){
	if ((eaddress1.value == "")|| (eaddress1.value.indexOf ('@') == -1)|| (eaddress1.value.indexOf ('.') == -1)) {
				return false;
				}
	if (!(eaddress1.value == eaddress2.value)){
				return false;
				}
return true;
}
// End of Function


// Function: checkCleantext(text). Simple Text input check, also removes possible trailing and leading white spaces.
// Arguments: Text string with possible leading and trailing white spaces.
// Returns: True=Text string exist and is cleaned up, False=If text string empty.
function checkCleantext (text) {
if (!(text.value == "")) {
	var temp = text;
  var obj = /^(\s*)([\W\w]*)(\b\s*$)/;
   	if (obj.test(temp)) { 
	 	temp = temp.replace(obj, '$2'); 
	 	}
  return temp;
	}
return false;
}
// End of Function



/* Dedicated Script Functions (Second Section):

checkDetails()

*/

// Function: checkDetails(). Main Function called to Handle & Check Details entered into Form
// Arguments: none.
// Returns: True: Details OK, False: Details not OK.

function checkDetails(){

var checkflag=true;
var cntWords=0;

// Get Known Quantities:
		var getHeadline = document.getElementById("headline");
		var getSdate = document.getElementById("sdate");
		var getEdate= document.getElementById("edate");
		var getTown = document.getElementById("town");
		var getPlace = document.getElementById("place");
		var getTime = document.getElementById("times");
		var getDesc = document.getElementById("descrip");
		var getPrice = document.getElementById("price");
		var getElink = document.getElementById("elink");
		var getCat = document.getElementById("cat");
		var getEmail = document.getElementById("eAddress");
		var getRepEmail = document.getElementById("repeAddress");
		var getFname = document.getElementById("fstname");
		var getSname = document.getElementById("surname");
	
// Check Headline Exists *Is Required
getHeadline.style.backgroundColor="White";
if (!(getHeadline.value=checkCleantext(getHeadline.value)) || (getHeadline.length > 50)) {
	getHeadline.style.backgroundColor="Yellow";
	checkflag=false;
}

// Check Start Date Exists and if its format is OK *Is Required
/*if (!(getSdate.value=checkCleantext(getSdate.value)) || !(isDate(getSdate))) {
	getSdate.style.backgroundColor="Yellow";
	checkflag=false;
}

// Check End Date Exists and if its format is OK
if ((getEdate.value=checkCleantext(getEdate.value))) {
	if (!(isDate(getSdate.value))) {
	getEdate.style.backgroundColor="Yellow";
	checkflag=false;
	}
}*/

// Check Town (only one word allowed!) *Is Required
getTown.style.backgroundColor="White";
//if (!(getTown.value=checkCleantext(getTown.value)) || !(oneWordOnly(getTown.value))) {
getTown.value=checkCleantext(getTown.value);
cntWords=getTown.value.split(" ");
if (!(getTown.value) ||  (cntWords.length > 1)) {
		getTown.style.backgroundColor="Yellow";
		checkflag=false;
}

// Check Place (only one word allowed!) *Is Required
getPlace.style.backgroundColor="White";
getPlace.value=checkCleantext(getPlace.value);
cntWords=getPlace.value.split(" ");
if (!(getPlace.value) ||  (cntWords.length > 1)) {
		getPlace.style.backgroundColor="Yellow";
		checkflag=false;
}

// Check Times Given *Is Required
getTime.style.backgroundColor="White";
getTime.value=checkCleantext(getTime.value);
if (!getTime.value) {
	getTime.style.backgroundColor="Yellow";
	checkflag=false;
}

// Check Description is not over 500 words long
getDesc.style.backgroundColor="White";
if (getDesc.value.length > 500) {
	getDesc.style.backgroundColor="Yellow";
	checkflag=false;
}

// Check Price Given *Is Required
getPrice.style.backgroundColor="White";
getPrice.value=checkCleantext(getPrice.value);
if (!getPrice.value) {
	getPrice.style.backgroundColor="Yellow";
	checkflag=false;
}

// Check E-mail *Is Required 
getEmail.value=checkCleantext(getEmail.value);
getRepEmail.value=checkCleantext(getRepEmail.value);
getEmail.style.backgroundColor="White";
getRepEmail.style.backgroundColor="White";
	if (!(checkEmailadrs (getEmail, getRepEmail))) {
	getEmail.style.backgroundColor="Yellow";
	getRepEmail.style.backgroundColor="Yellow"; 
	checkflag=false;
	}

// Check Firstname (only one word allowed!) *Is Required
getFname.style.backgroundColor="White";
getFname.value=checkCleantext(getFname.value);
cntWords=getFname.value.split(" ");
if (!(getFname.value) ||  (cntWords.length > 1)) {
	getFname.style.backgroundColor="Yellow";
	checkflag=false;
}

// Check Surname (only one word allowed!) *Is Required
getSname.style.backgroundColor="White";
getSname.value=checkCleantext(getSname.value);
var cntWords=getSname.value.split(" ");
if (!(getSname.value) ||  (cntWords.length > 1)) {
	getSname.style.backgroundColor="Yellow";
	checkflag=false;
}
return checkflag;
}
// End of Function
