/*
This script is designed to pre-populate any webform with a set of values from the url querystring.
each name value pair must correspond to an input element on the web page and each input item's
name must have exactly the same id value including case.

To implement this the html form must link to this js page and call the prepopulate() function.
the function will then extract all name value pairs from the querystring and link each name
to an input tag in the html and populate it with the respective value.

it is common to run the prepopulate function in an onload call either in the body tag or in 
an image tag lower down in the page to ensure that each element has been loaded before
attempting to pre-populate it.

So simply add into html page
<script language="JavaScript" type="text/javascript" src="http://uploadlibrary.com/andy.thorpe/listsignup.js"></script>
and the add into either the body tag or an image tag
onload="javascript:prepopulate();"

*/
	//populate the form
	function prepopulate(){
		//call the getParams function to get the name value pairs into an array
		var params = getParams();
			//make sure the params array actually contains values before running
			if (params.length >= 1)
			{			
				/*run in a try catch. IE and Firefox react differently, 
				sometimes their is always one parameter even if you can;t see one
				*/
				try
				{
					// loop through the parameeters in the array
					for (var i=0;i<params.length;i++)
					{					
						//split each name value pair into a key and value
						pair = params[i].split('=');
						var key = pair[0];
						var val = pair[1];
							//Get the input html element which corresponds with the key from the querystring
							//in a try catch in case a param does not match an element
							try
							{
								var x = document.getElementById(key);
							
								//if it is a text or a text area
								if ((x.type == "text")||("textarea"))
								{
									//if the value is not null or blank
									if (val != null)
									{
										//run the fixchars function on the vlaue and make it the value of the textbox/area
										x.value = fixchars(val);
									}//end if
								}//end if
								
								//if the inout type is a check box
								if (x.type == "checkbox")
								{
									//if it has a value
									if (val != null)
									{
										//check it. Only checkboxes that need checking should be in the query string.
										x.checked = true;
									}//end if
								}//end if

								//if the type is a collection of radio buttons
								if (x.type == "radio")
								{
									//get the radio buttons form
									var theform = x.form;
									//get the group of radio button with the same id from using the key
									var rad = theform.elements[key];
									//get the number of radio buttons in the group
									var radlen = rad.length;
									//loop through the radio group
									for (var i = 0;i<radlen;i++)
									{
										//if the radio button's value is the same as the value from the parameters
										if (rad[i].value == val)
										{
											//check it.
											rad[i].checked = true;
										}//end if
										else 
										{
											//if not, uncheck it
											rad[i].checked = false;
										}//end else
									}//end loop								
								}//end if radio

								//if the element is a drop down menu, of which only one choice can be selected
								if (x.type == "select-one")
								{
									//if there is a value
									if (val != null)
									{
										//loop through the drop down options
										for (var j=0;j<x.options.length;j++)
										{
											//if the option's value matches the value from the quesrystring
											if (x.options[j].value == val)
											{
												//make that option the selected one
												x.selectedIndex = j;
											}//end if
										}//end option loop
									}//end if
								}//end if
							
							}//end try getelement
							catch (err){
							null;
							}
					}//end element by id loop
				}//end try
				
				//if an error is thrown - usually due to there paramter count being one but having no real paramters??
				//catch the error...
				catch (err)
				{
					//and do nothing.
					null;
				}//end catch
			}//end if
	}//end function

	//this function gets the querystring part of the url (the bit ith and after the question mark)
	//and splits it into name value pairs and puts them into and arrry which gets returned.
	function getParams(){
		//get the querystring
		var qstring = window.location.search; 
		//take the question mark of the end.
		qstring = qstring.substring(1);
		//split the string up where there is an ampersand and put it inot an array.
		var params = qstring.split('&');
		//return the array.
		return params;
	}//end function

	
	//this function is here to fix the url replacement codes which are inserted into an url
	//so that it can handle punctuation and newlines etc.
	function fixchars(s){
		//list the most likely characters
		var qchars = new Array("+","%2C","%0D%0A","%21","%3F");
		//list the replacing characters, one each per url code
		var rchars = new Array(" ",",","\n","!","?");
		//put the number of chars in eacgh list into a variable
		charcount = qchars.length;
		
		//assign the parameter s to a local variable
		var strReplaceAll = s;
			//decode the string to use the built in replacement mechanism
			strReplaceAll = decodeURI(strReplaceAll);
			
			//loop through the array of url codes
			for (var i=0;i<charcount;i++ )
			{
				//get each character from both lists, so that one variable has the code and other the replacement
				qchar = qchars[i];
				rchar = rchars[i];
				//ger the index of the url code
				var intIndexOfMatch = strReplaceAll.indexOf(qchar);			
				//loop though the string until the index is -1, meaning there is no index for it, so it is not there
				while (intIndexOfMatch != -1){
					//replace the url code with the escape character
					strReplaceAll = strReplaceAll.replace( qchar, rchar );	 
					//re-assignthe index of the url code.
					intIndexOfMatch = strReplaceAll.indexOf( qchar );
				}//end while loop					
			}//end for loop		
			///return the fully punctuated string.
		return strReplaceAll;		
	}//end fixchars

	/*
	
	optional - email validation script

	*/

	function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   //alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   //alert("wrong @")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    //alert("wrong dot")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    //alert("@ error")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    //alert("dot error")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    //alert("dot2 error")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    //alert("has spaces")
		    return false
		 }

 		 return true
		 
	}//end echeck()