String.prototype.startsWith = function(str) {return (this.match("^"+str)==str)}

function changeLanguage(obj)
{
	window.location = base_url + 'business/changeLanguage/' + obj.options[obj.selectedIndex].value ;	
}

function addOnLoad(func) {
	if (window.onload) {
	var temp = window.onload;
	window.onload = function () {
	temp();
	func();
	}
	} else {
	window.onload = func;
	}
} 

function LoadSubCategory(obj, subCatSpanObj, subCatObj)
{
    var html = $.ajax({
        url: base_url + "ajax/sub_category/" + $(obj).val() + "/" + Date.parse(new Date()),
        async: false
     }).responseText;
     
    $(subCatSpanObj).html(html);
    
    //alert($(subCatObj + " option").length);
    if ($(subCatObj + " option").length > 1)
    {
        $(subCatObj).addClass("required");
    }
    else
    {
        $(subCatObj).removeClass("required");
    }
    
    /*
    $.ajax({
        
        url: base_url + "ajax/sub_category/" + $(obj).val(),
        async: false,
        cache: false,
        success: function(html){ 
                       $(subCatObj).html(html);
                        } 
     });
    */
}

var httpObject = null;
var messageFuture;

// Get the HTTP Object
  function getHTTPObject(){
     if (window.ActiveXObject) return new ActiveXObject("Microsoft.XMLHTTP");
     else if (window.XMLHttpRequest) return new XMLHttpRequest();
     else {
        alert("Your browser does not support AJAX.");
        return null;
     }
  }  
  
  function showMessage(){
     if(httpObject.readyState == 4){
        var response = httpObject.responseText;
        
        if (response != '')
        {
            //alert(response);
            if (!response.startsWith('<html>'))
            {   
                arrMessages = response.split("~|");
                
                for(i in arrMessages)
                {
                    arrMessageItems = arrMessages[i].split("~:");  
                    chatId = arrMessageItems[0];
                    name = arrMessageItems[1];
                    company = arrMessageItems[2];
                    link = arrMessageItems[3];
                    
                    messagePopUp(chatId, name, company, link);
                    window.focus();
                }
            }
        }
     }
  } 

function messageCheck()
 {
     httpObject = getHTTPObject();
     var randomnumber=Math.floor(Math.random()*10000);
     if (httpObject != null) {
        link = base_url + "ajax/check_message/"+randomnumber;
        httpObject.open("GET", link , true);
        httpObject.onreadystatechange = showMessage;
        httpObject.send(null);
     }
     
     messageFuture = window.setTimeout("messageCheck()", 10000);
 }


