/*
 * This contains random tools used by Synthesis Technologies, Inc. to 
 * maintain the elearningcurve site.
 *
 * Dependancies: Requires the jQuery 1.42 library
 * 
 * Created by:  Justin Grant
 */

function subscribeToWebinar() {
				if (getValue("webinar")=="") {
					alert("The current webinar is invalid.  Please refresh the page and try again.");
				} else {
					if(validateEmail(document.getElementById("email").value) == false) {
						alert("The current email is invalid.  Please check and resubmit.");
					}
					else {
						var query = 'insertEmail=' + webSafeString(document.getElementById("email").value) 
							+ '&webinar=' +  webSafeString(getValue("webinar"));
						jQuery.ajax({
							url:'/v/vspfiles/proxy.asp?' + query, 
							type: 'GET',
							success: function(html) {
								window.location = "register_confirmed.html";
							}, 
							error: function() {
								alert('There was a problem sending your message.  Please contact support@elc.com.');
							}
						});
					}
				}
			}
			
			function getValue(varname) {
				// First, we load the URL into a variable
				var url = window.location.href;
			
				//check to make sure variables are passed
				if(url.search("\\?")==-1) {
					return "";
				}
			  
				// Next, split the url by the ?
				var qparts = url.split("?");
				
				// Check that there is a querystring, return "" if not
				if (qparts.length == 0) {
					return "";
				}
			
				// Then find the querystring, everything after the ?
				var query = qparts[1];
			
				// Split the query string into variables (separates by &s)
				var vars = query.split("&");
			
				// Initialize the value with "" as default
				var value = "";
			
				// Iterate through vars, checking each one for varname
				for (i=0;i<vars.length;i++) {
					// Split the variable by =, which splits name and value
					var parts = vars[i].split("=");
			    
					// Check if the correct variable
				  if (parts[0] == varname) {
				  	// Load value into variable
				    value = parts[1];
			
				  	// End the loop
				  	break;
					}
				}
			  
				// Convert escape code
				value = unescape(value);
						  
				// Convert "+"s to " "s
				value.replace(/\+/g," ");
			
				// Return the value
				return value;
			}
			
			function validateEmail(str) {
				if (str == undefined || str == "") {
					return false;
				}
				var at="@"
				var dot="."
				var lat=str.indexOf(at)
				var lstr=str.length
				var ldot=str.indexOf(dot)
				
				if (str.indexOf(at)==-1)
					return false
		
				if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)
					return false
		
				if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)
					return false
		
				if (str.indexOf(at,(lat+1))!=-1)
					return false
		
			  if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot)
			  	return false
	
			  if (str.indexOf(dot,(lat+2))==-1)
			  	return false
			
			  if (str.indexOf(" ")!=-1)
			  	return false
		
		 		return true					
			}
			
			function webSafeString(val) {
				if (val == undefined)
					return "";
				var len     = val.length;
				var backlen = len;
				var i       = 0;
				
				var newStr  = "";
				var frag    = "";
				var encval  = "";
				var original = val;
				
				while (backlen > 0) {
	        lastpercent = val.lastIndexOf("%");
					// we found a % char. Need to handle
					if (lastpercent != -1) {
	        	// everything *after* the %
						frag = val.substring(lastpercent+1,val.length);
	          
	          // re-assign val to everything *before* the %
	          val  = val.substring(0,lastpercent);
	          
	          // end contains unencoded
	          if (frag.length >= 2) {
	          	//  alert ("frag is greater than or equal to 2");
	          	encval = frag.substring(0,2);
	          	newStr = frag.substring(2,frag.length) + newStr;
	            
	            //convert the char here. for now it just doesn't add it.
	          	if ("01234567890abcdefABCDEF".indexOf(encval.substring(0,1)) != -1 &&
	            	"01234567890abcdefABCDEF".indexOf(encval.substring(1,2)) != -1) {
								encval = String.fromCharCode(parseInt(encval, 16)); // hex to base 10
	              newStr = encval + newStr; // prepend the char in
	        		}
	        	}
	          // adjust length of the string to be examined
	      		backlen = lastpercent;
	          
					}
	      	else { 
	      		// if there is no %, just leave the value as-is
	      		newStr = val + newStr; backlen = 0; 
	      	} 
	     	} 
	      return newStr;
			}

