// JavaScript Document

function hideLanguageSelect(){
	checkCookie();
	
	document.getElementById('light').style.display='none';
	document.getElementById('fade').style.display='none';
}

function showLanguageSelect(){
	document.getElementById('light').style.display='block';
	document.getElementById('fade').style.display='block';
	window.onscroll = function(){self.scrollTo(0,0);}
}

function center_selector(){
	var w = 0;
	var h = 0;

	//IE
	if(!window.innerWidth){
		//strict mode
		if(!(document.documentElement.clientWidth == 0)){
			w = document.documentElement.clientWidth;
			h = document.documentElement.clientHeight;
		}
		//quirks mode
		else{
			w = document.body.clientWidth;
			h = document.body.clientHeight;
		}
	}
	//w3c
	else{
		w = window.innerWidth;
		h = window.innerHeight;
	}
	//return {width:w,height:h};
	
	width_of_selector = 435;
	height_of_selector = 300;
		
	center_width = (w / 2) - (width_of_selector / 2);
	center_height = (h / 2) - (height_of_selector / 2);

	//alert (centering);
	
	document.getElementById("light").style.left = center_width;
	document.getElementById("light").style.top = center_height;
}




function init_buttons(){
	button_us = document.getElementById('usa_button');
	h1_image_us = document.getElementById('usa_cookie');
	button_us.onmouseout = function () {h1_image_us.style.backgroundPosition ='0 0'};
	button_us.onmouseover = function () {h1_image_us.style.backgroundPosition ='0 -38px'};
	button_us.onclick = function () {createCookie('usa',20)};
	
	button_canada = document.getElementById('canada_button');
	h1_image_can = document.getElementById('canada_cookie');
	button_canada.onmouseout = function () {h1_image_can.style.backgroundPosition ='0 0'};
	button_canada.onmouseover = function () {h1_image_can.style.backgroundPosition ='0 -38px'};
	button_canada.onclick = function () {createCookie('ca_en',20)};
}


function switchBackImage(id){
	button = document.getElementById(id);
	button.style.backgroundPosition ='0 -38px';
};

function outBackImage(id){
	button = document.getElementById(id);
	button.style.backgroundPosition ='0 0';
};






// Setting the cookie or checking if it exists

function checkCookie(){
	var x = readCookie('solidifi_locale');
	if (x) {
				if (x=="ca_en") {location = "../ca_en/";}
				else if(x=="ca_fr") {location = "../ca_fr/";}
			}
			else {showLanguageSelect();}
}

function createCookie(value,days) {
	
	window.onscroll = function(){};
	
	name="solidifi_locale";
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
	
	hideLanguageSelect();
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}


// window.onload = checkCookie;

function init_language(){
	init_buttons();
	checkCookie();
	center_selector();
}


// Login Validation

function submit_form(){
	document.valuesLogin.submit();
}

function submit_form_init(){
	startScript();
}

function startScript()
{
	var fail_param = gup( 'fail' );
	if (fail_param == 'u'){
		document.getElementById('error_message').style.display = 'block';
		document.getElementById('register_txt').style.display = 'none';
	}
	
	button_code();

}

function gup( name )
{
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
	return "";
  else
	return results[1];
}


function button_code(){
	var button_obj = document.getElementById('login_btn');
	button_obj.onmouseover = rollover_btn;
	button_obj.onmouseout = rollout_btn;	
	button_obj.onclick = submit_form;
}

function rollover_btn(){
	document.getElementById("btn1_left").style.backgroundPosition = "0px 0px";
	document.getElementById("btn1_right").style.backgroundPosition = "0px 0px";
	document.getElementById("btn1_center").style.backgroundColor = "#505050";
	document.body.style.cursor= 'pointer';
}

function rollout_btn(){
	document.getElementById("btn1_left").style.backgroundPosition = "0px -27px";
	document.getElementById("btn1_right").style.backgroundPosition = "0px -27px";
	document.getElementById("btn1_center").style.backgroundColor = "#f58220";

	document.body.style.cursor= 'default';
}