// jsr_class.js
//
// JSONscriptRequest -- a simple class for making HTTP requests
// using dynamically generated script tags and JSON
//
// Author: Jason Levitt
// Date: December 7th, 2005
//
// A SECURITY WARNING FROM DOUGLAS CROCKFORD:
// "The dynamic <script> tag hack suffers from a problem. It allows a page 
// to access data from any server in the web, which is really useful. 
// Unfortunately, the data is returned in the form of a script. That script 
// can deliver the data, but it runs with the same authority as scripts on 
// the base page, so it is able to steal cookies or misuse the authorization 
// of the user with the server. A rogue script can do destructive things to 
// the relationship between the user and the base server."
//
// So, be extremely cautious in your use of this script.
//
//
// Sample Usage:
//
// <script type="text/javascript" src="jsr_class.js"></script>
// 
// function callbackfunc(jsonData) {
//      alert('Latitude = ' + jsonData.ResultSet.Result[0].Latitude + 
//            '  Longitude = ' + jsonData.ResultSet.Result[0].Longitude);
//      aObj.removeScriptTag();
// }
//
// request = 'http://api.local.yahoo.com/MapsService/V1/geocode?appid=YahooDemo&
//            output=json&callback=callbackfunc&location=78704';
// aObj = new JSONscriptRequest(request);
// aObj.buildScriptTag();
// aObj.addScriptTag();
//
//


// Constructor -- pass a REST request URL to the constructor
//
function JSONscriptRequest(fullUrl) {
    // REST request path
    this.fullUrl = fullUrl; 
    // Keep IE from caching requests
    this.noCacheIE = '&noCacheIE=' + (new Date()).getTime();
    // Get the DOM location to put the script tag
    this.headLoc = document.getElementsByTagName("head").item(0);
    // Generate a unique script tag id
    this.scriptId = 'JscriptId' + JSONscriptRequest.scriptCounter++;
}

// Static script ID counter
JSONscriptRequest.scriptCounter = 1;

// buildScriptTag method
//
JSONscriptRequest.prototype.buildScriptTag = function () {

    // Create the script tag
    this.scriptObj = document.createElement("script");
    
    // Add script object attributes
    this.scriptObj.setAttribute("type", "text/javascript");
    this.scriptObj.setAttribute("charset", "utf-8");
    this.scriptObj.setAttribute("src", this.fullUrl);
    this.scriptObj.setAttribute("id", this.scriptId);
}
 
// removeScriptTag method
// 
JSONscriptRequest.prototype.removeScriptTag = function () {
    // Destroy the script tag
    this.headLoc.removeChild(this.scriptObj);  
}

// addScriptTag method
//
JSONscriptRequest.prototype.addScriptTag = function () {
    // Create the script tag
    this.headLoc.appendChild(this.scriptObj);
}



function ka_createCookie(name,value,days) {
	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=/";
}

function ka_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;
}

function ka_eraseCookie(name) {
	ka_createCookie(name,"",-1);
}

function ka_colorbox_alert(inlineId,lightboxHtml){
	//alert('ka_colorbox_alert inlineId:'+inlineId+' html:'+$j('#'+inlineId).html());
	try{
		$j('#'+inlineId).html(lightboxHtml);		
		//alert($j('#'+inlineId).html());
		$j.fn.colorbox({inline:true, title:"", href:"#"+inlineId, transition:"elastic", speed:500, scrollbars:false, initialWidth:"100", initialHeight:"50", width:"300", height:"150", open:true, overlayClose:true, opacity:0.3});		
	}
	//if lightbox script is not loaded yet, keep trying again 1 second later
	catch(err){
		//setTimeout ("ka_colorbox_alert('"+inlineId+"','"+lightboxHtml+"')", 1000);
	}
}

function ka_colorbox_login(inlineId){
	try{
		$j.fn.colorbox({inline:true, title:"", href:"#"+inlineId, transition:"elastic", speed:500, scrollbars:false, initialWidth:"100", initialHeight:"50", width:"600", height:"440", open:true, overlayClose:true, opacity:0.3});		
	}
	//if lightbox script is not loaded yet, keep trying again 1 second later
	catch(err){
		//setTimeout ("ka_colorbox_login('"+inlineId+"')", 1000);
	}
}

function ka_refresh_page(){
	window.location.href = window.location.href;
}

function ka_check_email(str){
	var at="@";
	var dot=".";
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);
	if (str.indexOf(at)==-1){
	   return false;
	}
	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   return false;
	}
	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
	   return false;
	}
	if (str.indexOf(at,(lat+1))!=-1){
	   return false;
	}
	if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
	   return false;
	}
	if (str.indexOf(dot,(lat+2))==-1){
	   return false;
	}	
	if (str.indexOf(" ")!=-1){
	   return false;
	}
	return true;					
}

function ka_urldecode(str){
	return decodeURIComponent(str.replace(/\+/g,  " "));
}

function ka_urlencode(str){
	return encodeURIComponent(str);
}

function ka_loadRatingModule(url, title, id){
	  var ka_scriptUrl = KA_RATING_BASE_SITE+'/start.php?as='+KA_RATING_AS+'&url='+url+'&title='+title+'&id='+id;
	  $j.getScript(ka_scriptUrl);
}		  
