/**
* Search function para set.
*/
function setSearchCriteria(url) {
	SetCookie("GoogleSearch", document.form1.textfield.value, "1");
	location.href = url;
}

// Set Cookie
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()+"; path=/;domain=" + window.location.hostname + ";";
}

function ReadCookie(cookieName){
	var theCookie=""+document.cookie;
	var ind=theCookie.indexOf(cookieName);
	if (ind==-1 || cookieName=="") return ""; 
	var ind1=theCookie.indexOf(';',ind);
	if (ind1==-1) ind1=theCookie.length; 
	var val = unescape(theCookie.substring(ind+cookieName.length+1,ind1));
	return val;
}

function checkCookie() {	
	username = ReadCookie('HD');	
	if (username != null && username != ""){				
		document.sendForm.sendMe.disabled = false; 
	} else {
		document.sendForm.sendMe.disabled = true;			  
	}
}	

function callcookie(ActivityType, Activity_name) {	
	SetCookie('activity_type', ActivityType,'1')
	SetCookie('activity_name', Activity_name,'1')
}

/*
function setGlobalSponsor(htm) {
	if(htm != ""){
		if(document.getElementById("sponserimage"))
			document.getElementById("sponserimage").innerHTML = htm;
		if(document.getElementById("sponsertext"))
			document.getElementById("sponsertext").innerHTML = globalSponsor;
	}
	else{
		if(document.getElementById("sponsers-box"))
			document.getElementById("sponsers-box").style.display = 'none';
		if(document.getElementById("sponsertext"))
			document.getElementById("sponsertext").innerHTML = "";
		if(document.getElementById("sponserimage"))
			document.getElementById("sponserimage").innerHTML = "";
	}
}
*/
// Added new function to rotate multiple global sponsers
function setGlobalSponsor(globalArray) {
	if(globalArray != ""){
		var currentIndex = ReadCookie("globalSponsorIndex");
		if(currentIndex == "" || currentIndex == "NaN")
			var currentGlobalIndex = 0;
		else
			var currentGlobalIndex = currentIndex;
		var globalPhoto = "";
		var maxGlobalValue = 0;
		globalPhoto = globalArray.split("||");
		maxGlobalValue = globalPhoto.length;
		maxGlobalValue = maxGlobalValue - 2;	
		if(maxGlobalValue < 0)
			maxGlobalValue = 0;	

		if(document.getElementById("sponsertext"))
			document.getElementById("sponsertext").innerHTML = globalSponsor;
		if(document.getElementById("sponserimage"))
			document.getElementById("sponserimage").innerHTML = globalPhoto[currentGlobalIndex];
		
		if(currentGlobalIndex == maxGlobalValue)
			currentGlobalIndex = 0;
		else
			currentGlobalIndex = parseInt(currentGlobalIndex) + 1;
	
		SetCookie("globalSponsorIndex",currentGlobalIndex,365);
	} else {
		if(document.getElementById("sponsers-box"))
			document.getElementById("sponsers-box").style.display = 'none';
		if(document.getElementById("sponsertext"))
			document.getElementById("sponsertext").innerHTML = "";
		if(document.getElementById("sponserimage"))
			document.getElementById("sponserimage").innerHTML = "";
	}
}

/**
 * Check the cookie, to find out if user already logged in or not.
 * If Logged  in change the logged in link to Logout ..
 * @TODO   logout functionality.
 * @author Ashok Das 
 * @since 08 March 2008
 */
function checkLoginStatus() {
	username = ReadUID('HD');
	var loginWidget = document.getElementById('login_widget');
	
	if(loginWidget != null)	{
		if (username != null && username != "") {
			loginWidget.innerHTML = '<label><a href="'+SiteURLforJS+'/profile.html?UID='+username+'" >'+myAccount+'</a></label><label>&nbsp;/&nbsp;<a href="javascript:gSLAuthProxy.__Render(\'/Authentication/Logout.rails?reloadPage=true\')" rel=\'lightbox\'>'+logout+'</a></label>';
		} else {
			loginWidget.innerHTML = '<label><a href="javascript:showPopup(\'login\')" rel="lightbox">'+login+'</a></label><label>&nbsp;/&nbsp;<a href="javascript:showPopup(\'Reg\')" rel="lightbox" class="blue-normal"><b>'+register+'</b></a></label>';
		}
	}
}

function ReadUID(cookieName){
	var theCookie=""+document.cookie;
	var ind=theCookie.indexOf(cookieName);
	if (ind==-1 || cookieName=="") return ""; 
	var ind1=theCookie.indexOf(';',ind);
	if (ind1==-1) ind1=theCookie.length; 
	var val = unescape(theCookie.substring(ind+cookieName.length+1,ind1));
	var uid=val.split("|");
	val=uid[0];
	return val;
}

function flashFlag(){
	var flashTime = ReadCookie("flash_flag");
	if(flashTime==""){
		SetCookie("flash_flag", "FirstTime", "1");
		return 1;
	}
	if(flashTime=="FirstTime"){
		return 0;
	}
}

function setSiteSponsor(htm) {
	if(htm != ""){
		if(document.getElementById("SiteSponsorImage"))
			document.getElementById("SiteSponsorImage").innerHTML = htm;
		if(document.getElementById("SiteSponsorText"))
			document.getElementById("SiteSponsorText").innerHTML = "<h3>" + siteSponsor + "</h3>";
	}else{
	    if(document.getElementById("SiteSponsor"))
			document.getElementById("SiteSponsor").style.display = 'none';
	    if(document.getElementById("SiteSponsorText"))
			document.getElementById("SiteSponsorText").innerHTML = "";
	    if(document.getElementById("SiteSponsorImage"))
			document.getElementById("SiteSponsorImage").innerHTML = "";
	}
}

function display(id_img , compTitle , compTitleState) {
	 	var compTitleStateInit=compTitleState;
	 	if(compTitleState!="")
			compTitleState=compTitleState.substring(0,compTitleState.length-1);
		var stateArr=compTitleState.split("~");
		
	 	var compTitleInit=compTitle;
	 	if(compTitle!="")
			compTitle=compTitle.substring(0,compTitle.length-1);
		var cityArr=compTitle.split("~");
		var displayCityName="";
		var dataTop="";
		var dataBottom="";
		for(var i =0;i<cityArr.length;i++){
			var imgTop="imageTopDiv_"+cityArr[i];
			var imgBottom="imageBottomDiv_"+cityArr[i];
			var img="imageDiv_"+cityArr[i];
			var bdy="bodydiv_"+cityArr[i];
			
			if(i>id_img || i<id_img){
				dataBottom+="<div class='dynamic-container-bg'>";
				dataBottom+="<div class='dynamic-img-arrow'><img src='"+ SiteURLforJS + "/resources/images/left-img-arrow.gif' alt='' /></div>";
				dataBottom+="<div class='dyn-link'><a href=\"javascript:display("+i+",'"+compTitleInit+"','"+compTitleStateInit+"')\" onClick=\"document.getElementById('flashcontent').style.display = 'none';\">"+cityArr[i]+"</a></div>";
				dataBottom+="</div>";
			}
			if(i==id_img){
				displayCityName=cityArr[i];
				document.getElementById(img).style.display = 'block';
				document.getElementById(bdy).style.display = 'block';
			}
			else{
				document.getElementById(img).style.display = 'none';
				document.getElementById(bdy).style.display = 'none';
			}
			document.getElementById(imgTop).innerHTML="";
			document.getElementById(imgBottom).innerHTML="";
		}
		var selImgTop="imageTopDiv_"+displayCityName;
		var selImgBottom="imageBottomDiv_"+displayCityName;
		document.getElementById(selImgTop).innerHTML=dataTop;
		document.getElementById(selImgBottom).innerHTML=dataBottom;
		if(dataTop=="")
			document.getElementById(selImgTop).style.display = 'none';
		if(dataBottom=="")
			document.getElementById(selImgBottom).style.display = 'none';
}

function activityReset(){
	if (document.activity_filter_form) {
		for(var i=0;i<document.activity_filter_form.length;i++){
			var cBox=document.activity_filter_form[i];
			cBox.checked=false;
		}
	}
}

/**
*	  This method is used to validate EmailID format and also checks if emailID exist or not.
*/

function ValidateForm(){
	var httpRequestNews;
	document.getElementById("srcHTMLNew").style.display = "none";
	var emailID=document.myForm.myemail.value;
	emailID=emailID.trim();
	if ((emailID==null)||(emailID=="")){
			document.getElementById("srcHTML").innerHTML = enterEmailId;
			document.myForm.myemail.focus();
			return;
	}
	if (!echeck(emailID)){
			emailID.value="";
			document.myForm.myemail.focus();
			return;
	}
	document.getElementById("srcHTML").innerHTML = waitMsg;

	var url = SiteURLforJS + '/RegisterEmail';
	var xmlHttpReq = false;
	var self = this;
	// Mozilla/Safari
	if (window.XMLHttpRequest) {
		self.xmlHttpReq = new XMLHttpRequest();
	}
	// IE
	else if (window.ActiveXObject) {
		self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
	}
	if (!self.xmlHttpReq) {
		alert('Giving up :( Cannot create an XMLHTTP instance) Please check the Browser Settings');
		return false;
	}
	self.xmlHttpReq.open('POST', url, true);
	self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded;');
	self.xmlHttpReq.onreadystatechange = function() {
		if (self.xmlHttpReq.readyState == 4) {
			if (self.xmlHttpReq.status == 200) {
				str=self.xmlHttpReq.responseText;
				document.getElementById("srcHTML").innerHTML = str;
			}else{
				document.getElementById("srcHTML").innerHTML = validateForm_Text;
               }
		}
	} ; 
	self.xmlHttpReq.send("emailID=" + emailID + "&timestamp=" + new Date().getTime() + "&languageCode=" + language_Code + "&countryCode=" + country_Code);
}

/**
 *  This function is used to check the format of Email Id Input
 */
function echeck(str) {
	document.getElementById("srcHTMLNew").style.display = "none";
	var at="@";
	var dot=".";
	var comm=",";
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);
	if (str.indexOf(at)==-1){
		   document.getElementById("srcHTML").innerHTML = emailAlert;
		   return false;
	}
	
	if (str.indexOf(comm) > -1){
	   document.getElementById("srcHTML").innerHTML = emailAddress;
	   return false;
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   document.getElementById("srcHTML").innerHTML = emailAlert;
	   return false;
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		document.getElementById("srcHTML").innerHTML = emailAlert;
		return false;
	  }

	 if (str.indexOf(at,(lat+1))!=-1){
		document.getElementById("srcHTML").innerHTML = emailAddress;
		return false;
	   }

	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		document.getElementById("srcHTML").innerHTML = emailAlert;
		return false;
	 }

	 if (str.indexOf(dot,(lat+2))==-1){
		document.getElementById("srcHTML").innerHTML = emailAlert;
		return false;
	 }
	
	 if (str.indexOf(" ")!=-1){
		document.getElementById("srcHTML").innerHTML = emailAlert;
		return false;
	 }
	 return true;
}