//-----------------
//wp base library
//---------------

//**************************
//Global Variables
//**************************

var wp={};
var wpGV_BrowserNotAdvance = "Your browser does not support some advance features. Please upgrade to a higher version browser.";



//**************************
//Global Constants
//**************************

var wpGC_MSXmlHttp = 'Microsoft.XMLHTTP';



//**************************
//Shortcuts
//**************************

function wp$(i){return document.getElementById(i)};



//**************************
//DOM Specifics
//**************************

document.getElementsByClassName = function(className){
	var ret = new Array();
    var elements = document.getElementsByTagName("*");
    for(var i = 0;i < elements.length;i++){
		if(elements[i].className.indexOf(" ") >= 0){
			var classes = elements[i].className.split(" ");
			for(var j = 0;j < classes.length;j++){
				if(classes[j] == className)
					ret.push(elements[i]);
			}
		}
		else if(elements[i].className == className)
			ret.push(elements[i]);
		}
	return ret;
}

document.getElementsLikeClassName = function(className){
	var ret = new Array();
    var elements = document.getElementsByTagName("*");
    for(var i = 0;i < elements.length;i++){
		if(elements[i].className.indexOf(" ") >= 0){
			var classes = elements[i].className.split(" ");
			for(var j = 0;j < classes.length;j++){
				if(classes[j].indexOf(className) >= 0)
					ret.push(elements[i]);
			}
		}
		else if(elements[i].className.indexOf(className) >= 0)
			ret.push(elements[i]);
		}
	return ret;
}



//**************************
//AJAX Specific
//**************************

//wp Ajax request function
function wpAjaxReq(url, method, postParam, cacheable, async, onReady){
    //PARAM
        //url: location of ajax target
        //method: GET | POST
        //postParam: querystring representation if POST method is used
        //cacheable: true | false
        //async: true | false
        //onReady: function parameter for xmlhttp.onreadystatechange
			//State Description 
			//0 The request is not initialized 
			//1 The request has been set up 
			//2 The request has been sent 
			//3 The request is in process 
			//4 The request is complete 
    //RETURN
        //xmlhttp object
    
	//ajax call here...
	var xmlhttp;
	if(window.XMLHttpRequest)
	{	// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	}
	else if (window.ActiveXObject)
	{	// code for IE6, IE5
		xmlhttp=new ActiveXObject(wpGC_MSXmlHttp);
	}
	else
	{	alert(wpGV_BrowserNotAdvance);
	}
	xmlhttp.onreadystatechange=function(){onReady(xmlhttp)};
	
	var ajaxDate = new Date();
	
	//== If url query string contain ? dont use ? for the AjaxTime
	var myUrlStr= url.indexOf('?');
	var myTime='?t='; 
	if (myUrlStr != '') {
		myTime = '&t=';
	};
	//==
	
	//Param passing to server side processing
	xmlhttp.open(
		method,
		url + (cacheable ? '':myTime + ajaxDate.getTime()),
		async
	);
	xmlhttp.send(postParam);
	
	return xmlhttp;
}



//**************************
//Data Type Specific
//**************************

//C Like string formatting
String.format = function(text)
{
    //check if there are two arguments in the arguments list
    if ( arguments.length <= 1 )
    {
        //if there are not 2 or more arguments there’s nothing to replace
        //just return the original text
        return text;
    }
    //decrement to move to the second argument in the array
    var tokenCount = arguments.length - 2;
    for( var token = 0; token <= tokenCount; token++ )
    {
        //iterate through the tokens and replace their placeholders from the original text in order
        text = text.replace( new RegExp( "\\{" + token + "\\}", "gi" ), arguments[ token + 1 ] );
    }
    return text;
};



//**************************
//User Interface Specific
//**************************

//screen info
wp.page=function(){
	return{
		top:function(){return document.body.scrollTop||document.documentElement.scrollTop},
		width:function(){return self.innerWidth||document.documentElement.clientWidth},
		height:function(){return self.innerHeight||document.documentElement.clientHeight},
		theight:function(){
			var d=document, b=d.body, e=d.documentElement;
			return Math.max(Math.max(b.scrollHeight,e.scrollHeight),Math.max(b.clientHeight,e.clientHeight))
		},
		twidth:function(){
			var d=document, b=d.body, e=d.documentElement;
			return Math.max(Math.max(b.scrollWidth,e.scrollWidth),Math.max(b.clientWidth,e.clientWidth))
		}
	}
}();



//**************************
//Data Posting Specific
//**************************

function wpSelectPostBackWithQueryParam(postBackQueryParamName, postBackQueryParamValue)
{ 
			var myURL = window.location.href;
			var myURLParts = myURL.split('?');
			var myURLQueryParts = null;
			if(myURLParts.length > 1)
				myURLQueryParts = myURLParts[1].split('&');

			myURLNew = myURLParts[0] + '?';
			var i;
			for(i=0; i<myURLQueryParts.length; i++){
				var myQueryParamParts = null;
				myQueryParamParts = myURLQueryParts[i].split('=');
				
				if(myQueryParamParts[0].toLowerCase() != postBackQueryParamName.toLowerCase()){
					myURLNew = myURLNew + myURLQueryParts[i] + '&';
					}
			}
			
			myURLNew = myURLNew + postBackQueryParamName + '=' + postBackQueryParamValue;
			//use href to keep url history; replace does not keep history for some browsers.
			window.location.href = myURLNew;
}

function wpGetQueryParamValue(paramName)
{ 
		var myURL = window.location.href;
		var myURLParts = myURL.split('?');
		if(myURLParts.length > 1){
			var myURLQueryParts = null;
			myURLQueryParts = myURLParts[1].split('&');

			var i;
			for(i=0; i<myURLQueryParts.length; i++){
				var myQueryParamParts = null;
				myQueryParamParts = myURLQueryParts[i].split('=');
				
				if(myQueryParamParts[0].toLowerCase() == paramName.toLowerCase()){
					return myQueryParamParts[1];
					}
			}
		}
		return null;
}
