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) );

document.cookie = name + "=" +escape( value ) +
( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
( ( path ) ? ";path=" + path : "" ) + 
( ( domain ) ? ";domain=" + domain : "" ) +
( ( secure ) ? ";secure" : "" );
}


// this function gets the cookie, if it exists
function Get_Cookie( name ) {
	
var start = document.cookie.indexOf( name + "=" );
var len = start + name.length + 1;
if ( ( !start ) &&
( name != document.cookie.substring( 0, name.length ) ) )
{
return null;
}
if ( start == -1 ) return null;
var end = document.cookie.indexOf( ";", len );
if ( end == -1 ) end = document.cookie.length;
return unescape( document.cookie.substring( len, end ) );
}

var reloadLoc;

function setLang(formID){
	if (formID == null){
		formID = "form";
 	}
 	langForm = document.getElementById(formID);	
	var newlang = "_en";
	var gotoReloadLoc = false;
	var val = 0;
	if (langForm.langRadio[1].checked){
		newlang = "_es";
		val = 1;
	}
	var cooky = Get_Cookie( "lang" );
	if (cooky == null){
		Set_Cookie( "lang", newlang, 365, "/", "", "");
		if (val == 1){
			gotoReloadLoc = true;
		}
	}
	else {
		if (newlang != cooky){
			gotoReloadLoc = true;
			Set_Cookie( "lang", newlang, 365, "/", "", "");
		}
	}
	if (gotoReloadLoc){
		window.location ="/"+reloadLoc;
	}
}


function setLangOneClick(formID){
		if (setLangCooky(formID)){
//		alert ("reload loc:" +reloadLoc);
//		alert("current loc:" + window.location);
		//window.location ="/"+reloadLoc;
//		alert("going to "+reloadLoc + " now");
		window.location = reloadLoc;
	}
	else {
//		alert ("cookie val is "+ cooky);
	}

}


function setLangSession(formID){
if (formID == null){
	formID = "form";
 }
 langForm = formID;
/* 	alert("elem type is:"+document.f8.elem_type.value+" "+parentFrame.getElemType()+" "+parentFrame.test);
	return;*/
 // inside your form you must have the line <input type="hidden" name="page_id" value=112>
  http.open('post', 'index.php', true);
  http.onreadystatechange = handleLangData; //CHANGE THIS FUNCTION TO CHANGE TO multipart/form-data if a file is to be uploaded
  http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  http.send(buildQueryString(formID));   
//  alert(buildQueryString(formID));
}

 function handleLangData(){
   if(http.readyState == 4){ //Finished loading the response
     if (http.responseText.indexOf("ERROR") >= 0){
       alert(http.responseText + " " + http.responseText.indexOf("RROR"));
     }
     else {
	// 	alert(http.responseText);
		i = 0;
		parse_text = http.responseText;
	//	alert("parse_text: "+parse_text);
		i = parse_text.indexOf("<ret_stat>")+10;
		ok = parse_text.substring(i, parse_text.indexOf("</ret_stat>"));
		if (ok == "OK"){
			i = parse_text.indexOf("<redirect>");
			if (i != -1){
				i += 10;
				redirect = parse_text.substring(i, parse_text.indexOf("</redirect>"));
			//	alert(redirect);
				window.location = redirect;
			}
		}
	}
  hideCurrentPopup();
  }
 }
function setLangSessionOneClick(formID){
if (formID == null){
	formID = "form";
 }
 langForm = formID;
 setLangCooky(formID);
/* 	alert("about to send data");
	return;*/
 // inside your form you must have the line <input type="hidden" name="page_id" value=112>
  http.open('post', 'index.php', true);
  http.onreadystatechange = handleLangData; //CHANGE THIS FUNCTION TO CHANGE TO multipart/form-data if a file is to be uploaded
  http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  http.send(buildQueryString(formID));   
//  alert(buildQueryString(formID));
}

function setLangCooky(formID){
	if (formID == null){
		formID = "form";
 	}
// 	alert ("setting language from form "+formID +" going to "+reloadLoc);
 	langForm = document.getElementById(formID);	
	var newlang = "_en";
	var gotoReloadLoc = false;
	var val = 0;
	try {
		if (langForm.langRadio[0].value == 1){
			newlang = "_es";
			val = 1;
		}
	}
	catch (error) {
		if (langForm.langRadio.value == 1){
			newlang = "_es";
			val = 1;
		}
	}		
	var cooky = Get_Cookie( "lang" );
	if (cooky == null){
		Set_Cookie( "lang", newlang, 365, "/", "", "");
		if (val == 1){
			gotoReloadLoc = true;
		}
	}
	else {
		if (newlang != cooky){
			gotoReloadLoc = true;
			Set_Cookie( "lang", newlang, 365, "/", "", "");
		}
	}
	return gotoReloadLoc;
}	