// JavaScript Document

var fid;
var home;
var flashBig = true;

function addFlashNav(small,index)
{
	if(small)
	{
		if(index)
		{
			 writeFlash("flashNav/navigationSmallHome.swf", "800", "150", "nav");
			// document.body.style.backgroundImage = "url(images2/mainbackground.jpg)";
			 //document.body.style.backgroundPosition = "0 120px";
		}else{
			 writeFlash("../flashNav/navigationSmall.swf", "800", "150", "nav");
			// document.body.style.backgroundImage = "url(../images2/mainbackground.jpg)";
			// document.body.style.backgroundPosition = "0 100px";
		}
		
	}else{
		writeFlash("../flashNav/navigation.swf", "800", "230", "nav");
		//document.body.style.backgroundImage = "url(../images2/mainbackgroundbig.jpg)";
		//document.body.style.backgroundPosition = "0 100px";
	}
}


function writeFlash(path, width, height, id){
	fid = id;
	document.write('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="' + width + '" height="' + height + '" id="' + id + '" align="top">');
	document.write('<param name="allowScriptAccess" value="sameDomain" />');
	document.write('<param name="allowFullScreen" value="false" />');
	document.write('<param name="align" value="top" />');
	document.write('<param name="salign" value="t" />');
	document.write('<param name="movie" value="' + path + '" />');
	document.write('<param name="scale" value="noscale" />');
	document.write('<param name="wmode" value="transparent" />');
	document.write('<param name="quality" value="high" />');
	document.write('<param name="bgcolor" value="#ffffff" />');
	document.write('<embed src="' + path + '" quality="high" scale="noscale" salign="t" bgcolor="#ffffff" width="' + width + '" height="' + height + '" align="top" name="' + id + '"align="middle" allowScriptAccess="sameDomain"  wmode="transparent" allowFullScreen="false" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />');
	document.write('</object>');
}

function thisMovie(movieName) {
    	if (navigator.appName.indexOf("Microsoft") != -1) {
	 	return window[movieName];
    	} else {
 		return document[movieName];
    	}
}

function toActionScript(value) {
      thisMovie(fid).sendToActionScript(value);
}

function getLocation(){
   	var fm = thisMovie(fid);
	toActionScript(home);
	if(home == true){
		flashBig = true;
		document.body.style.backgroundImage = "url(images2/mainbackground.jpg)";
   		fm.setAttribute("height", "150");
	}else{
		flashBig = false;
		document.body.style.backgroundImage = "url(images2/mainbackgroundbig.jpg)";
		fm.setAttribute("height", "230");
	}
}




function checkNav(){

	if(flashBig == true && home == false){
		getLocation();
	}else if(flashBig == false && home == true){
		getLocation();
	}
}

function addcss(index){

	var browser = navigator.appName; 
	var filename;
	if(browser == "Microsoft Internet Explorer"){
		filename = "iestyle.css";
	}else{
		filename = "style.css";
	}
	
	var fileref=document.createElement("link");
   	fileref.setAttribute("rel", "stylesheet");
    fileref.setAttribute("type", "text/css");
	if(index)
	{
		
    	fileref.setAttribute("href", filename);
	}else{
		fileref.setAttribute("href", "../" + filename);
	}
	document.getElementsByTagName("head")[0].appendChild(fileref);
}


///////////////////////////////////////////////////////////////////
///////////LOGIN TO RELAY//////////////////////////////////////////
///////////////////////////////////////////////////////////////////

	var isFileShare = false;
	var isProjects = false;

	var url = 'relay/relay.php';
	var mainsite = 'relay/relay.html';		
	//var ajax = new Ajax.Request(url, {onSuccess: userLogin_handler, method: 'post', parameters: 'relay=checkLogin'});

	function fakeLogin()
	{
		loginGraphic();
		var params = $H({ relay: 'userLogin', username: $('username').value, password: $('password').value });
		var ajax = new Ajax.Request(url,{
			onSuccess: userLogin_handler,
			method: 'post', 
			parameters: params.toQueryString()
		});
	}
	
	function userLogin_handler(response)
	{ 
		var json_data = response.responseText;
		eval("var jsonObject = ("+json_data+")");
		var status = jsonObject.bindings[0];
			//alert(json_data);
			//return false;
		if(status.login == 'true')
		{
			isFileShare = true;
			//document.location = mainsite;
		}
		checkIfProjects();
	}
	
	function checkIfProjects()
	{
		var params = $H({ type: "login", username: $('username').value, password: $('password').value });
		var ajax = new Ajax.Request("projectsdb/application/ajaxhandler.php",{
			onSuccess: userLogin_handler2,
			method: 'post', 
			parameters: params.toQueryString()
		});
	}
	
	function userLogin_handler2(response)
	{
		if(response.responseText != 0)
		{
			isProjects = true;
		}
		
		if(isProjects && isFileShare)
		{
			document.location = "back";
		}
		else if(isProjects && !isFileShare)
		{
			document.location = "projectsdb/login";
		}
		else if(!isProjects && isFileShare)
		{
			document.location = mainsite;
		}
		else
		{
			invalidLogin();
		}
	}

var lastInvalid = false;

function loginGraphic(){
	var lg = document.getElementById("login");
	var btn = document.getElementById("submitbtn"); 
	var ldr = document.getElementById("loader"); 
	var us = document.getElementById("username"); 
	var pw = document.getElementById("password"); 
	var newText;
	var newSpan;
	
	btn.style.backgroundImage = "url(images2/loginsubmitbtndown.jpg)";
	btn.style.color = "#999999";
	ldr.style.visibility ="visible";
	if(lastInvalid == true){
		
		var invalid = document.getElementById("invalid");
		lg.removeChild(invalid);
		invalid = false;
	}
}

function invalidLogin(){

	var lg = document.getElementById("login");
	var btn = document.getElementById("submitbtn"); 
	var ldr = document.getElementById("loader"); 
	var us = document.getElementById("username"); 
	var pw = document.getElementById("password"); 
	var newText;
	var newSpan;
	
	lastInvalid = true;
	btn.style.backgroundImage = "url(images2/loginsubmitbtn.jpg)";
	btn.style.color = "#666666";
	ldr.style.visibility ="hidden";
	pw.value = "";
	us.value = "";	
	newText = document.createTextNode("invalid login");
	newSpan = document.createElement("span");
	newSpan.id = "invalid";
	newSpan.style.position = "absolute";
	newSpan.style.left = "75px";
	newSpan.style.fontWeight = "bold";
	newSpan.style.top = "9px";
	newSpan.style.color = "red";
	newSpan.appendChild(newText);
	lg.appendChild(newSpan);
}

////////////////////////////////////////////////
///////Email Contract Form//////////////////////
////////////////////////////////////////////////

function requestInfo()
{
	var params = $H({ name: $('name').value, email: $('email').value, company: $('company').value, emailcopy: $('emailbody').value });

	var ajax = new Ajax.Request("../mailer.php",{
		onSuccess: onRequestSuccess,
		method: 'get', 
		parameters: params.toQueryString()
	});
}

function onRequestSuccess(success)
{	
	if(success.responseText  == "yes")
	{
		
		$('requestform').reset();
		alert("Thank you for contacting Re-Systems Group USA.\n We will contact very sortly in response to your message.");
	}
	else
		alert("We are sorry, There was an error sending your request.");
		
	
}