/*
	A collection of utility functions for the i711 website
*/


//Umm.. this sets a cookie
function Set_Cookie( name, value, expires, path, domain, secure ) 
{
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );

	/*
	if the expires variable is set, make the correct 
	expires time, the current script below will set 
	it for x number of days, to make it for hours, 
	delete * 24, for minutes, delete * 60 * 24
	*/
	if ( expires ){
		expires = expires * 1000 * 60 * 60 * 24;
	}
	var expires_date = new Date( today.getTime() + (expires) );
	//alert(name + ' ' + expires_date.toGMTString());
	document.cookie = name + "=" +escape( value ) +
	( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
	( ( path ) ? ";path=" + path : "" ) + 
	( ( domain ) ? ";domain=" + domain : "" ) +
	( ( secure ) ? ";secure" : "" );
}


// toggels a Div and sets a cookie using div id if requested.
var toggleDiv = function(cBox, target ,cookie){
	if(cBox.checked){
		$(target).style.display = "inline";
		if(cookie)Set_Cookie( target, 'inline', 30, '/', '', '' ) ;
		}
	else{
		$(target).style.display = "none";
		if(cookie)Set_Cookie( target, 'none', 30, '/', '', '' ) ; 
		}
}

// a quick, less bulky way to do our AJAX calls
var quickJAX = function(url ,tokens, handler, method){
		//var url = '/inc/remote_scripts/register.php';
		var request = $H(tokens);
		var pars = request.toQueryString();
		if(method == '')method = 'get';
		if(handler == ''){alert('please add a method to catch ajax return');return;}
		//alert(pars);
		var myAjax = new Ajax.Request(
			url, 
			{
				method: method, 
				parameters: pars, 
				onComplete: handler
			});
}

// a quick, less bulky way to do our AJAX calls
var quickJAXV = function(url ,pars, handler, method){
		//var url = '/inc/remote_scripts/register.php';
		//var request = $H(tokens);
		//var pars = request.toQueryString();
		if(method == '')method = 'get';
		if(handler == ''){alert('please add a method to catch ajax return');return;}
		//alert(pars);
		var myAjax = new Ajax.Request(
			url, 
			{
				method: method, 
				parameters: pars, 
				onComplete: handler
			});
}



var captureMe = function(originalRequest){
	alert(originalRequest.responseText);
}

var setValue = function(id,val){//if you wanna be really lazy setting a value then this is for you =)
	$(id).value = val;
}


/********************************************************************************

	The following code was ripped out of /inc/htmlHeader.inc

*********************************************************************************/

//similar to php trim , will trim leading and ending white space
var fulltrim = function(mString) {
	String(mString);	
	return mString.replace(/(?:(?:^|\n)\s+|\s+(?:$|\n))/g,"").replace(/\s+/g," ");
}

//returns the checked value on a given radio group
var getCheckedButton = function(group, form) {
	if (typeof group == 'string') group = form.elements[group];
	for (var i = 0, n = group.length; i < n; ++i)
		if (group[i].checked) return group[i];
	return form.elements[group];
}


function validateForm(theForm,checkMail){
	if(theForm.chkIAgree&& theForm.chkIAgree.checked == false){
		alert("You must Agree to the Terms & Conditions to continue");
		return false;
	}
	if(checkMail == 1){
		if(theForm.email.value != theForm.email2.value){
			alert("Your email adresses do not match. Please make sure you correctly enter your email address.");
			return false;
		}
		if(theForm.passwd.value != theForm.passwd2.value){
			alert("Passwords do not match. Please make sure you correctly enter your password.");
			return false;
		}
	}
		noInput = '';
		for(i=0;i<=reqArray.length-1;i++){
			//alert("Checking out "+reqArray[i]);
			if(eval('theForm.'+reqArray[i]+'.value')==''){
			noInput += labelArray[i] + "\n";
		}
		if(reqArray[i] == 'email'){
			var email = eval('theForm.'+reqArray[i]+'.value');
			if (!isValidEmailAddress(email)){
				alert("You must enter a valid, working email address. Your account cannot be activated without it.");
				return false;
			}
		}
	}
	if(noInput != ''){
		alert("The following required fields were left blank:\n"+noInput+"\nPlease correct the problem and re-submit.");
		return false;
	}else{
		return true;
	}
}


function validateForm2(theForm,checkMail){
	//alert(theForm.name);
	if(theForm.chkIAgree && theForm.chkIAgree.checked == false){
		//alert("You must Agree to the Terms & Conditions to continue");
		//return false;
	}
	
	if(checkMail == 1){
		if(theForm.emailAdress.value != theForm.email2.value){
			alert("Your email adresses do not match. Please make sure you correctly enter your email address.");
			return false;
		}
		if(reqArray.indexOf('passwd')>= 0){
		if(theForm.passwd.value != theForm.passwd2.value){
			alert("Passwords do not match. Please make sure you correctly enter your password.");
			return false;
		}
		}
	}
	
	
		noInput = '';
		
		for(i=0;i<=reqArray.length-1;i++){
			var a = ['system' , 'connection', 'vrs', 'browser', 'processor','memory'];
			var theArray = $A(a);
			if(theArray.indexOf(reqArray[i]) > -1){
				//alert(reqArray[i]);
				var nodeCheck = getCheckedButton(reqArray[i] , theForm);
				if (nodeCheck == undefined){
					//node = new Object;
					var node = ''
				}
				else{
					node = nodeCheck.value;
				}
				
			}
			else{
			var node = eval('theForm.'+reqArray[i]+'.value');
			}
		if(node==''){
			noInput += labelArray[i] + "\n";
		}
		
		if(reqArray[i] == 'emailAdress'){
			var email = eval('theForm.'+reqArray[i]+'.value');
			if (!isValidEmailAddress(email)){
				alert("You must enter a valid, working email address. Your account cannot be activated without it.");
				return false;
			}
		}
	}
	if(noInput != ''){
		alert("The following required fields were left blank:\n"+noInput+"\nPlease correct the problem and re-submit.");
		return false;
	}else{
		return true;
	}
}



function isValidEmailAddress(email) {
	var re = new RegExp("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$");
	if (re.test(email.toLowerCase())){
		return true;
	}
	else {
		return false;
	}
}

  
	

