/**
 * Autopopulate the usergenerated form field by TJ
 * THIS CODE IS SPECIFIC TO ROCK UPLOAD FORM (not same as other autopopulate_form.js)
 *
 * Script adapted from http://www.456bereastreet.com/archive/200710/autopopulating_text_input_fields_with_javascript/
 */
var autoPopulate = {
	sInputClass:'autopopthis', // Class name for input elements to autopopulate
	
	/**
	 * Main function
	 */
	init:function() {
		// Check for DOM support
		if (!document.getElementById || !document.createTextNode) {return;}
		
		// Find all input elements with the given className
		var arrInputs = autoPopulate.getElementsByClassName(document, '*', autoPopulate.sInputClass);
		var iInputs = arrInputs.length;
		var oInput;
		

		for (var i=0; i<iInputs; i++) {
			oInput = arrInputs[i];
			
			// Make sure it's a text input or select drop-down box
			if (oInput.type == 'text') {
				
				// fire the Focus event for the input
				oInput.select();
				
			} else if (oInput.type == 'select-one') { 
				
				// fire the Focus event for the select 
				oInput.focus();
			}
		}
	},
	/**
	 * getElementsByClassName function included here for portability.
	 * Remove if you are already using one.
	 * Written by Jonathan Snook, http://www.snook.ca/jonathan
	 * Add-ons by Robert Nyman, http://www.robertnyman.com
	 */
	getElementsByClassName:function(oElm, strTagName, strClassName) {
	    var arrElements = (strTagName == "*" && document.all)? document.all : oElm.getElementsByTagName(strTagName);
	    var arrReturnElements = new Array();
	    strClassName = strClassName.replace(/\-/g, "\\-");
	    var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
	    var oElement;
	    for(var i=0; i<arrElements.length; i++){
	        oElement = arrElements[i];      
	        if(oRegExp.test(oElement.className)){
	            arrReturnElements.push(oElement);
	        }   
	    }
	    return (arrReturnElements)
	},
	/**
	 * addEvent function included here for portability.
	 * Remove if you are already using an addEvent/DOMReady function.
	 * Found at http://www.quirksmode.org/blog/archives/2005/10/_and_the_winner_1.html
	 */
	addEvent:function(obj, type, fn) {
		if (obj.addEventListener)
			obj.addEventListener(type, fn, false);
		else if (obj.attachEvent) {
			obj["e"+type+fn] = fn;
			obj[type+fn] = function() {obj["e"+type+fn](window.event);}
			obj.attachEvent("on"+type, obj[type+fn]);
		}
	},
	/**
	 * getQueryVariable gets value of requested querystring variable
	 */
	getQueryVariable:function(variable) {
		
		var query = window.location.search.substring(1);
		
		
		var vars = query.split("&");
		
		
		for (var i=0;i<vars.length;i++) {
			
			var pair = vars[i].split("=");
			
			if (pair[0].toLowerCase() == variable.toLowerCase()) {
				
			  	return pair[1];
			}
			
		} 
		return "";
	}
};

/**
 * Init on window load.
 * Replace this with a call to your own addEvent/DOMReady function if you use one.
 */
autoPopulate.addEvent(window, 'load', RunTheseFunctionsOnLoad);


/**
 * Function to fill the text boxes by TJ
 * Called by onfocus event on text input boxes
 */
function fillTextBox(objTextBox, strQueryStringVar) {
	if(objTextBox) {
		//Set the currently focuses textbox text to the value of the variable (strQueryStringVar) from the querystring
		
		var strQueryStringValue = Url.decode(autoPopulate.getQueryVariable(strQueryStringVar))
		
		if (strQueryStringValue != '') {
			objTextBox.value = strQueryStringValue
		}
		// PROBLEM WITH DISABLING A TEXT BOX WHEN SUBMITTING DATA ENTRY MODULE
		// if there is value for the requested variable it will be placed in the textbox using the method above
		// so disable the text box since it is already populated.
		//if (autoPopulate.getQueryVariable(strQueryStringVar) != "") {
		//	objTextBox.disabled = true;
		//}
	}
}


/**
 * Function to select item in dropdown by TJ
 * Called by onfocus event on dropdown select box
 */
function fillDropDropBox(objDropDropBox, strQueryStringVar) {
	
	if(objDropDropBox) {
		//Set the currently focuses textbox text to the value of the variable (strQueryStringVar) from the querystring
		//objDropDropBox.value = Url.decode(autoPopulate.getQueryVariable(strQueryStringVar))

		

		for (i = 0;i < objDropDropBox.length; i++) {
			
			if (Url.decode(autoPopulate.getQueryVariable(strQueryStringVar)) == objDropDropBox.options[i].text){
				objDropDropBox.options[i].selected = true; //select the option in the dropdown
				/*document.getElementById("category_select").style.visibility = 'hidden';	//hide the drop down*/
				return;

			}
		}
	}
}




/** ---------------------------------------- **/
/** Code specific to DAI'S PROTEGE promotion **/
/** ---------------------------------------- **/

function checkUploadMethod() {
	
	var strUploadMethod = Url.decode(autoPopulate.getQueryVariable('uploadmethod'))
	
	if(strUploadMethod.toLowerCase() == "youtube") {
		window.location = "http://www.c4tv.co.nz/OnTV/Shows/DaisProtege/XmodThankYouPage/tabid/899/language/en-US/Default.aspx"	
	} 
	else if (strUploadMethod.toLowerCase() == "c4_upload") {
		/*not used*/
	}
	
}




function getTitleMessage() {
	
	var strTitleMessage = Url.decode(autoPopulate.getQueryVariable('message'))
	
	if(strTitleMessage != "") {
		if(document.getElementById) {
		
			document.getElementById("TitleMessage").innerHTML = strTitleMessage
			
		}
	}
}




function RunTheseFunctionsOnLoad() {
	
	autoPopulate.init()
	
	checkUploadMethod()
	getTitleMessage()
}

