// HELP/INFO/DEVELOPER CONTACT: jose@jcao.com
// WATCH VIDEO POPUP

	// WATCH VIDEO POPUP

		function watchVideo(url) {

			window.open(url,'video','height=280,width=305,resizable=0,scrollbars=0,location=0,toolbar=0,status=0,left=100,top=100,screenX=100,screenY=100').focus;
		}



	//// Form Validation
		function frmValidate(frm) {
		// split rf by ":" to get the required field and type pairs
		// split required field and type pairs. first element of the 
		// array is the name of the required field. second element of the
		// array is the field type, 1=text field, 2=dropdown field, 3=emailadrress, 4=radio
			
			var rf = frm.elements['rf'].value;
			var err = "";
			var arr_rf = rf.split(':');
			for (i=0; i<arr_rf.length; i++) {
				var arr_ft = arr_rf[i].split('*');
				var fieldname = arr_ft[0];
				var fieldtype = arr_ft[1];
				
				var thisfield = frm.elements[fieldname];
				
				if (err != "") err += ".";
								
				switch (fieldtype){
					case "1": 
						
						if (thisfield.value == "") 	{
							document.getElementById(fieldname).style.backgroundColor='#FF9999';
							err += fieldname;
						}
						else 
							document.getElementById(fieldname).style.backgroundColor='#FFFFFF';
						break;
					case "2": 
						
						if (thisfield.options[thisfield.selectedIndex].value == "") {
							document.getElementById(fieldname).style.backgroundColor='#FF9999';			
							err += fieldname;
						}
						else
							document.getElementById(fieldname).style.backgroundColor='#FFFFFF';
						break;
					case "3": 						
						var valid = true;
						if (thisfield.value.length == 0) 
							valid = false;
						else {
							if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(thisfield.value) == false)
								valid = false;
						}	
						
						if (valid == false) {
							document.getElementById(fieldname).style.backgroundColor='#FF9999';
							err += fieldname;
						}
						else
							document.getElementById(fieldname).style.backgroundColor='#FFFFFF';
						break;
					case "4": 
						var selected = false;
						
						for(var i = 0; i < thisfield.length; i++) {
							if(radio[i].checked) {
								selected = true;
								break;
							}
						}
						if (selected == false) {
							document.getElementById(fieldname).style.backgroundColor='#FF9999';
							err += fieldname;
						}
						else
							document.getElementById(fieldname).style.backgroundColor='#FFFFFF';
						break;
					default : ;
						
				}		
			
			}		
			
			if (err != "") {
				document.getElementById('err').style.display='block';
				document.getElementById('nonerr').style.display='none';		
				return false;	
			}	
			else {
				var query = window.location.search.substring(1);
				frm.action = frm.action + "&" + query;				
				return true;
			}
		}


	//// HANDLES QUERYSTRINGS
		function queryHandler() {
			var classRoot = this;
			classRoot.values = new Array();
		// populate the values array
			classRoot.getValues = function() {
				var querystring   = document.location.search;
				classRoot.values.length = 0;
				if (querystring) {
					if (querystring.charAt(0) == '?') { querystring = querystring.substring(1, querystring.length); }
					// loop through pairs and assign them in the array
					for (loop = 0; loop < querystring.split('&').length; loop++) {
						var thisPair = querystring.split('&')[loop];
						var thisName = unescape(thisPair.split('=')[0]);
						var thisValue = unescape(thisPair.split('=')[1]);
						classRoot.values[thisName] = thisValue;
					}
				}
			}
		// get a specific value
			classRoot.getValue = function(getKey) {
				var thisValue = classRoot.values[getKey];
				return thisValue;
			}
			classRoot.getValues();
		}




	//// HANDLES COOKIES
		function cookieHandler() {
			var classRoot = this;
			classRoot.values = new Array();
		// populate the values array
			classRoot.getValues = function() {
				var cookieString   = document.cookie;
				classRoot.values.length = 0;
				if (cookieString) {
				// loop through pairs and assign them in the array
					for (loop = 0; loop < cookieString.split('; ').length; loop++) {
						var thisPair = cookieString.split('; ')[loop];
						var thisName = unescape(thisPair.split('=')[0]);
						var thisValue = unescape(thisPair.split('=')[1]);
						classRoot.values[thisName] = thisValue;
					}
				}
			}
		// get a specific value
			classRoot.getValue = function(getKey) {
				var thisValue = classRoot.values[getKey];
				return thisValue;
			}
		// write a cookie
			classRoot.write = function(cookieName, cookieValue, daysToExpire, path) {
				var cookieValue   = cookieName + '=' + escape(cookieValue);
				var cookieExpires = '';
				// date handling could be much more precise but this will do for now  ...
				var cookieDate = new Date();
				if (daysToExpire) {
					cookieDate.setTime(cookieDate.getTime()+(daysToExpire*24*60*60*1000));
					cookieValue+= ';expires=' + cookieDate.toGMTString();
				}
				if (path) { cookieValue+= path; }
				document.cookie = cookieValue;
				classRoot.getValues();
			}
			classRoot.getValues();
		}




	//// THIS FUNCTION MERGES TWO QUERYSTRINGS. THE FIRST TWO ARGUMENTS ARE REQUIRED (domQstr and subQstr). THESE ARE
	//// THE TWO QUERYSTRINGS YOU WANT TO MERGE. (domQstr) HAS DOMINANCE OVER (subQstr), WHICH IS TO SAY, IF THE SAME
	//// VARIABLE EXISTS IN BOTH QUERYSTRINGS, THE DOMINANT VALUE PERSISTS.
	//// 
	//// THE LAST ARGUMENT (omitQstr) IS OPTIONAL. THE (omitQstr) VARIABLE CAN BE USED TO PREVENT ADDING CERTAIN
	//// VARIABLES FROM (subQstr) TO THE FINAL STRING (PRESUMING THEY ARE NOT ALLREADY NAMED IN THE DOMINANT STRING).
	//// THIS VARIABLE SHOULD BE SUPPLIED AS AN AMPERSAND DELIMITED STRING WHERE EACH KEY REPRESENTS A VARIABLE NAME TO
	//// BE IGNORED. VERY HANDY WHEN YOU NEED TO FORWARD QUERYSTRINGS THROUGHOUT A SITE VIA FRONT-END DOM.
		function mergeQuerystrings(domQstr, subQstr, omitQstr){
			// create an array containing all vars to be ignored
				var deadVars = (omitQstr) ? omitQstr.split('&') : new Array(0); 
			// strip out all non-data information, make sure we are only dealing with data.
				var domQstr = (domQstr.indexOf('?') != -1) ? domQstr.split('?')[1] : domQstr;
				var subQstr = (subQstr.indexOf('?') != -1) ? subQstr.split('?')[1] : subQstr;
			// 1 - define some utilities to slim down our code.
			// this is a utility that converts querystrings into 2-dimensional arrays
				var parseToArray = function(url) {
					var qsIndex  = url.indexOf('?') + 1;
					var qsDelim  = (url.indexOf('&amp;') != -1) ? '&amp;' : '&';
					var fullQsStr = url.split('?')[1];
					if (qsIndex != 0 && fullQsStr != '') {
						var qsStr   = url.substring(qsIndex);
						var qsArray = qsStr.split(qsDelim)
						for (var loop in qsArray) { 
							var indexSplit = unescape(qsArray[loop]).split('=');
							qsArray[loop] = new Array();
							qsArray[loop]['name']  = indexSplit[0];
							qsArray[loop]['value'] = indexSplit[1];
						}
					} else {
						// return an empty array if there is on querystring
						var qsArray = new Array();
					}
					return qsArray;
				}
			// this is a utility that searches an array created by parseToArray() for a key
			// if it is found it returns an index value, otherwise it returns boolean false
				var keyIndex = function(arr, key) {
					var indexValue = false;
					for (var loop in arr) { if (arr[loop]['name'] == key) { indexValue = loop; } }
					return indexValue;
				}
			// this is a more simple array search function - one dimensional and only returns boolean
				var inArray = function(arr, key) {
					var indexValue = false;
					for (var loop in arr) { if (arr[loop] == key) { indexValue = true; } }
					return indexValue;
				}
			// 1 - make an array of all name/value pairs in the request being sent via domQstr
				var swfQsArray = parseToArray(domQstr);
			// 2 - make an array of all name/value pairs the loaded page request was made with
				var urlQsArray = parseToArray(subQstr);
			// 3 - compare arrays and add any nodes from urlQsArray to swfQsArray if not allready present. (don't overwrite hardcoded values from the .swf)
				for (var loop in urlQsArray) {
					var thisName = urlQsArray[loop]['name'];
					var thisNameKey = keyIndex(swfQsArray, thisName);
					var prohibitKey = inArray(deadVars, thisName);
					if (!thisNameKey && !prohibitKey) { swfQsArray.push(urlQsArray[loop]); }
				}
			// 4 - encode/prepare outgoing url
				var newQueryString = '';
				for (var loop in swfQsArray) {
					newQueryString+= escape(swfQsArray[loop]['name']) + '=' + escape(swfQsArray[loop]['value']);
					if (loop < swfQsArray.length - 1) { newQueryString+= '&'; }
				}
				var mergedString = (newQueryString != '') ? '?' + newQueryString : newQueryString;
			// return processed url.
				return newQueryString;
		}




	//// OBJECT INITIATION
		var QS     = new queryHandler();
		var COOKIE = new cookieHandler();





