// JavaScript Document

var uc = "";

function SetCookie(cookieName,cookieValue,nDays) {
	var today = new Date();
 	var expire = new Date();
 	if (nDays==null || nDays==0) nDays=1;
 		expire.setTime(today.getTime() + 3600000*24*nDays);
 	document.cookie = cookieName+"="+escape(cookieValue) + ";expires="+expire.toGMTString();
}

function getCookie(c_name) {
	if (document.cookie.length>0) {
  		c_start=document.cookie.indexOf(c_name + "=");
  		if (c_start!=-1) { 
    		c_start=c_start + c_name.length+1 ;
    		c_end=document.cookie.indexOf(";",c_start)
    		if (c_end==-1) c_end=document.cookie.length
    			return unescape(document.cookie.substring(c_start,c_end))
    	} 
  	}
return ""
}


//Change row color on sub_listing.tpl.php on mouseOver and mouseOut
function rowChange(elm){
	elm.style.background="#F7F7F7";
	elm.style.color="#000000";
}

function rowRestore(elm, rowColor){
	elm.style.background="#FFFFFF";
}


// load url for div onclick event
function load(u){
	location=u;
}

// hide / show div elements in advertising page
function loadTab(elmt) {
	var toHide=document.getElementById('main_content_banners');
	if (toHide) {toHide.style.display="none";}
	var toHide=document.getElementById('main_content_sponsorship');
	if (toHide) {toHide.style.display="none";}
	var toHide=document.getElementById('main_content_specs');
	if (toHide) {toHide.style.display="none";}	
	var toHide=document.getElementById('banners_info');
	if (toHide) {toHide.style.display="none";}
	var toHide=document.getElementById('sponsorship_info');
	if (toHide) {toHide.style.display="none";}
	
		
	var toShow=document.getElementById('main_content_'+elmt);
	if (toShow) {toShow.style.display="block";}
	
	var toShow=document.getElementById(elmt + '_info');
	if (toShow) {toShow.style.display="block";}	
}


// validate recommend to a friend form
function recommend_validate(){

var msg;
var msgflag;

msgflag = "false";
msg="The following fields require values:\n";

 if(document.forms['recommendform'].name.value == ""){
	msg+="name \n";
	msgflag="true";
	}

 if(document.forms['recommendform'].email.value == ""){
	msg+="email\n";
	msgflag="true";
	}

if(msgflag == "true"){
	msg+="\n\n Press OK button below to return the form";
     	alert(msg);
	//scroll(0,0);
	return false;
}

if(msgflag == "false"){
return true;
}

}


// validate mailer form
function mailer_validate(){

var msg;
var msgflag;

msgflag = "false";
msg="The following fields require values:\n";

 //if(document.forms['mailerform'].name.value == ""){
	//msg+="name \n";
	//msgflag="true";
	//}

 if(document.forms['mailerform'].email.value == ""){
	msg+="email\n";
	msgflag="true";
	}

if(msgflag == "true"){
	msg+="\n\n Press OK button below to return the form";
     	alert(msg);
	//scroll(0,0);
	return false;
}

if(msgflag == "false"){
return true;
}

}


var http_request = false;

function makePOSTRequest(code, url, parameters) {
    if (window.XMLHttpRequest) { // Mozilla, Safari,...
       http_request = new XMLHttpRequest();
       if (http_request.overrideMimeType) {
          http_request.overrideMimeType('text/xml');
    	}
    } else if (window.ActiveXObject) { // IE
    	try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
         } catch (e) {
            try {
               http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
         }
      }
      
	if (!http_request) {
    	alert('Cannot create XMLHTTP instance');
        return false;
    }
      
    if (code == '1' ) {
		http_request.onreadystatechange = processRecommend;
	}
	else if (code == '2' ) {
		http_request.onreadystatechange = processGetCountry;
	}
    http_request.open('POST', url, true);
    http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    http_request.setRequestHeader("Content-length", parameters.length);
    http_request.setRequestHeader("Connection", "close");
    http_request.send(parameters);  
}


// process XMLHttpRequest response for recomment form post
function processRecommend() {
   if (http_request.readyState == 4) {
        if (http_request.status == 200) {
            result = "your message has been sent";
			document.getElementById('recommend_result').style.visibility='visible';
            document.getElementById('recommend_result').innerHTML = result;
			setTimeout("document.getElementById('recommend_result').style.visibility='hidden';",5000);          
         } else {
            alert('There was a problem with the request.');
         }
    }
}


// process XMLHttpRequest response for geoip get country function
function processGetCountry(){
	var result = "";
	var country = "";
	SetCookie("iD-uc","",0);
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			var xmldoc = http_request.responseXML;
	        var x = xmldoc.getElementsByTagName("countryName");
			for(i=0;i<x.length;i++) {
				country = x[i].childNodes[0].nodeValue;
				SetCookie("iD-uc",result,0);
			}
			
		}
		else {
            alert('There was a problem with the request.');
		}
	}
}

// public recommend by email function
function get(obj) {
	if (recommend_validate()){	
    	var poststr = "&t=3" +
	  	"&f=" + encodeURI(document.getElementById("senderemail").value ) +
	  	"&n=" + encodeURI( document.getElementById("name").value )+
		"&e=" + encodeURI( document.getElementById("email").value )+
		"&w=" +	
		"&m=" +
		"&ip="+ encodeURI( document.getElementById("ip").value ) ;
      	makePOSTRequest('1', '/call/contact.nsf/recommend?openagent'+poststr, poststr);
	}
}


// private (admin) emailing function
function get2(obj) {
	if (mailer_validate()){	
    	var poststr = "&t=3" +
	  	"&f=" + encodeURI(document.getElementById("senderemail").value ) +
	  	"&n=" + encodeURI( document.getElementById("name").value )+
		"&e=" + encodeURI( document.getElementById("email").value )+
		"&w=" +	
		"&m=" +
		"&ip="+ encodeURI( document.getElementById("ip").value ) ;
      	makePOSTRequest('1', '/call/contact.nsf/idmailmarketing?openagent'+poststr, poststr);
	}
}


function getCountry(ip) {
	var poststr = "?ip=" + ip;
	makePOSTRequest('2', '/geoip/'+poststr, poststr);
}
