
/////////////// -- Begin of Editable Region -- ///////////////

/*************************************************************************
*
*  required Element Array
*  true == required
*
*  To find out indices of form elements set the showFormElementsActive
*  variable to true and call the signup.php. A popup with the correct
*  requiredElements array will appear.
*  Just copy and paste the whole array between the marked lines and set
*  required fileds to true
*
**************************************************************************/



//-- replace from below this line --//

//-- replace to above this line --//


// Set to true to determine indicies of form elements for requiredElements array 
var showFormElementsActive = false;

//color in which the required Labels should light up on alert
//set to named colour or hex code #rrggbb
//surround by double quotes e.g.: "FFFF00" or "#FF0" or "yellow"

// alert label color
var alertColour = "#A34053";

// regular label color
var normalColour = "#000000";

// define id of alert message to appear when input is missing
var alertName = "alertmsg";

// define the name of the Form you want to check
var formName = "Signup_Form";

// limitation of characters in comments field
var maxComments = 255;


/////////////// -- End of Editable Region -- ///////////////


//define other global variables
var rlength = requiredElements.length-2;
var items;
var alertmsg;
var thisform;

if(!defaultValues){var defaultValues = new Array();}

//init funtion will be called on page load
//window.onload = init;

function init() {
   alertmsg = (document.getElementById(alertName))?document.getElementById(alertName):'';
   //hide the alert message
   alertmsg.style.visibility = "hidden";
   alertmsg.style.color = alertColour;
   
   thisform = document.forms[formName];
   items = thisform.elements;

    /*************************************************************************
    *  Use showFormElements(items); to determine indicies of form elements
    **************************************************************************/
    if(showFormElementsActive){showFormElements(items);}
    
    /*************************************************************************/

    //grab default values from input fields
    if (defaultValues.length<1) {
        //thisform.reset();
        
		
		for (i = 0; i < items.length-1; i++) {
            defaultValues[i] = items[i].value;
        }
    }
    for (i=0; i < rlength; i++) {
        if (document.getElementById(items[i].name))
            document.getElementById(items[i].name).style.color = normalColour;
    }

}

//function called from the Signup Form onsubmit="testForm(this)"
function testForm(thisform) {
    var wasmissing;
    //check for Required Elements
    if(wasmissing = checkRequired()){
        //check for valid E-Mail
        wasmissing = (checkMail(thisform))? wasmissing : false;
    }

   //return false if any of the checks failed
   return wasmissing
}


// Function to compare required elements against
// their default value or empty input field.
// Returns false if element hasn't been changed.
function checkRequired () {
    var missing = true;
    
    
    for(i=0; i<rlength; i++) {
        if (items[i].type == "radio") {
								var tmpmissing = false;
								var j = i;
								while (items[j].name == items[i].name) {
									if (items[j].checked) tmpmissing = true;
									j++
								};
								if (!tmpmissing)
									document.getElementById(items[i].name).style.color = alertColour;
								else
									document.getElementById(items[i].name).style.color = normalColour;
								wasmissing = tmpmissing;
								i = j;
								continue;
							}
				  else if (requiredElements[i]) {
            
            if (items[i].value == "" && document.getElementById(items[i].name)){
            
                document.getElementById(items[i].name).style.color = alertColour;
                missing = false;
                
            } else if (items[i].name.indexOf("[") > -1 && items[i].type == "checkbox"){
            
                var j = 0;
                var tempcheck = false;
                var tempname = items[i].name;
                
                do {
                    if(items[tempname].checked){
                        tempcheck = true;
                    }
                    tempname = tempname.slice(0,tempname.lastIndexOf("["));
                
                }while(items[++i].name == (tempname += "["+ ++j +"]"))
                --i; //reduce i by 1 to compensate for the added while loop - not elegant but works
                tempname = tempname.slice(0,tempname.lastIndexOf("["));
                if(!tempcheck){
                    document.getElementById(tempname).style.color = alertColour;
                    missing = false;
                } else {
                    document.getElementById(tempname).style.color = normalColour;
                }
            }
            else {
                if (document.getElementById(items[i].name))
                    document.getElementById(items[i].name).style.color = normalColour;
            }
        }
        
    }
   if (!missing && alertName != "")
      alertmsg.style.visibility = "visible";
   return missing;
}


// function to check for valid e-mail address
function checkMail (thisform) {
   var email = thisform.elements['email'].value;
   var emailConf = thisform.elements['email_confirm'].value;

   if (email.indexOf("@") < 2) {
      alert("Please enter a valid email address!")
      return false
   }
   if (email != emailConf) {
      alert("Email addresses don't match!")
      return false
   }
   return true
}


// function called from checkbox or radio button to show hidden text input fields
// usage: onClick="showElement(this, ['element'{,element}])" more elements possible comma separated
function showElement(trigger, target) {
      for (i=0; i<target.length; i++) {
         document.getElementById(target[i]).style.visibility = "visible";
      }
}


// function called from checkbox or radio button to hide hidden text input fields
function hideElement(trigger, target) {
   for (i=0; i<target.length; i++) {
         document.getElementById(target[i]).style.visibility = "hidden";
      }
}

// function to limit characters in textarea(s)
function sliceComments(textfield){

    if(textfield.value.length > maxComments){
        textfield.value = textfield.value.substring(0, maxComments);
    }
}

// show all Form Elements in alert window with according indizes
function showFormElements(elementArray){
    
    var elength = elementArray.length;
    var output = "";
    
    for (i=0; i<elength; i++) {
      output += "requiredElements["+ i + "]" + ((i<10) ? " ": "") + "=\tfalse\t";
      output += "//" + elementArray[i].name + "\n";
    }
   
    win2 = window.open(); //open blank window and write to it
    win2.document.open(); //open document stream
    win2.document.write("<div><p>By default no elements are required.<br>"+
                        "Change value of required elements to true.</p>"+
                        "<p>Just copy below array and replace the requiredElements<br>"+
                        "array in the formscript.js with it.</p></div><hr>"+
                        "<div><pre>"+output+"</pre></div><hr>");
    win2.document.close();
}
//eof