// JavaScript Document
var NS4 = (navigator.appName == "Netscape" && parseInt(navigator.appVersion) < 5);

function addOption(theSel, theText, theValue)
{
	var newOpt = new Option(theText, theValue);
	var selLength = theSel.length;
	theSel.options[selLength] = newOpt;
	theSel.options[selLength].innerHTML = theText;
}

function deleteOption(theSel, theIndex)
{ 
	var selLength = theSel.length;
	if(theIndex == null && selLength >0 ){			
			for(var i=selLength-1; i>=0; i--){				
				theSel.options[i] = null;
			}
	}else{
		if(selLength>0)
		{
			theSel.options[theIndex] = null;
		}
	}
}
					
function moveAllOption(theSelFrom, theSelTo)
{
	var selLength = theSelFrom.length;
	var selectedText = new Array();
	var selectedValues = new Array();
	var selectedCount = 0;
	var i;

	// Find the selected Options in reverse order
	// and delete them from the 'from' Select.
	for(i=selLength-1; i>=0; i--)
	{
		selectedText[selectedCount] = theSelFrom.options[i].text;
		selectedValues[selectedCount] = theSelFrom.options[i].value;
		deleteOption(theSelFrom, i);
		selectedCount++;
	}

	// Add the selected text/values in reverse order.
	// This will add the Options to the 'to' Select
	// in the same order as they were in the 'from' Select.
	for(i=selectedCount-1; i>=0; i--)
	{
		addOption(theSelTo, selectedText[i], selectedValues[i]);
	}

	if(NS4) history.go(0);
}
							
function moveOptions(theSelFrom, theSelTo)
{
	var selLength = theSelFrom.length;
	var selectedText = new Array();
	var selectedValues = new Array();
	var selectedCount = 0;
	
	var i;

	// Find the selected Options in reverse order
	// and delete them from the 'from' Select.
	for(i=selLength-1; i>=0; i--)
	{
		if(theSelFrom.options[i].selected)
		{
			selectedText[selectedCount] = theSelFrom.options[i].text;
			selectedValues[selectedCount] = theSelFrom.options[i].value;
			deleteOption(theSelFrom, i);
			selectedCount++;
		}
	}

	// Add the selected text/values in reverse order.
	// This will add the Options to the 'to' Select
	// in the same order as they were in the 'from' Select.
	for(i=selectedCount-1; i>=0; i--)
	{
		addOption(theSelTo, selectedText[i], selectedValues[i]);
	}

	if(NS4) history.go(0);
}

function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	
	minuschar =  x1.indexOf("-");
	
	if(minchar){
		x1 = x1.substr(0,-1);
		ch_minus = "-";
	}
	else{
		ch_minus = "";	
	}
	
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return ch_minus + x1 + x2;
}

function createDIV(cid, cname, cvalue, cssname, ccommand){
	var inObj =document.createElement("DIV");
	inObj.id =cid;
	inObj.innerHTML =cvalue;
	inObj.className =cssname;
	inObj.onclick=function(){  eval(ccommand); }
	return inObj;
}

function createSPAN(cid, cname, cvalue, cssname, ccommand){
	var inObj =document.createElement("SPAN");
	inObj.id =cid;
	inObj.innerHTML =cvalue;
	if(cssname!='') inObj.className =cssname;
	inObj.onclick=function(){  eval(ccommand); }
	return inObj;
}

function createSELECT(cid, cname, cvalue, cssname, ccommand, copt, ckey, cval, cindex){
	var inObj =document.createElement("SELECT");
	inObj.id =cid;
	inObj.name =cname;
	if(cssname!='') inObj.className =cssname;
	inObj.onchange=function(){ eval(ccommand); }
	if(copt!=''){
		for(j=0;j<copt.length;j++)
		  {
			var newopt=document.createElement('option');
			if(j==cindex) newopt.selected =true;

			if(typeof(copt[j])=='undefined'){
				newopt.value='';
				newopt.appendChild(document.createTextNode(" ")); 
			}else{
				if(ckey!=''){
					newopt.value=copt[j][ckey]; 
					newopt.appendChild(document.createTextNode(copt[j][cval]));
					 
				}else{
					newopt.value=copt[j];	
					newopt.appendChild(document.createTextNode(copt[j])); 
				}
			}
			inObj.appendChild(newopt);
		}
	}
	return inObj;
}

function createSELECTwithINDEX(cid, cname, cvalue, cssname, copt, ckey, cval, cindex){
	var inObj =document.createElement("SELECT");
	inObj.id =cid;
	inObj.name =cname;
	inObj.className =cssname;

	if(copt!='' && copt!=null){
		for(j=0;j<copt.length;j++)
		  {
			var newopt=document.createElement('option');
			if(j==cindex) newopt.selected =true;
			if(ckey!=''){
				newopt.value=copt[j][ckey];
				newopt.innerHTML=copt[j][cval];
		  	}else{
				newopt.value=copt[j];
				newopt.innerHTML=copt[j];
			}
			inObj.appendChild(newopt);
		}
	}
	return inObj;
}

/* USE IN BOOKING */
function createSELECTBOOKING(cid, cname, cvalue, cssname, ccommand, copt, ckey, cval, cindex){
	var inObj =document.createElement("SELECT");
	inObj.id =cid;
	inObj.name =cname;
	inObj.className =cssname;
	inObj.onchange=function(){ eval(ccommand); }
	if(copt!=''){
		for(j=0;j<copt.length;j++)
		  {
			var newopt=document.createElement('option');
			if(j==cindex) newopt.selected =true;

			if(typeof(copt[j])=='undefined'){
				newopt.value='';
				newopt.appendChild(document.createTextNode(" ")); 
			}else{
				if(ckey!=''){
					newopt.value=copt[j][ckey]; 
					newopt.appendChild(document.createTextNode(copt[j][cval]));
					 
				}else{
					newopt.value=copt[j];	
					newopt.appendChild(document.createTextNode(copt[j])); 
				}
			}
			inObj.appendChild(newopt);
		}
	}
	
	return inObj;
}

function createINPUTHIDDEN(cid, cname, cvalue, cssname, ccommand){
	var inObj =document.createElement("INPUT");
	inObj.id =cid;
	inObj.name =cname;
	inObj.type ="hidden";
	inObj.value =cvalue;
	inObj.className =cssname;
	inObj.onclick=function(){  eval(ccommand); }
	return inObj;
}

function createLABEL(cid, cname, cvalue, cssname, ccommand){
	var inObj =document.createElement("LABEL");
	inObj.id =cid;
	inObj.innerHTML =cvalue;
	inObj.className =cssname;
	inObj.onclick=function(){  eval(ccommand); }
	return inObj;
}

function createRADIO(cid, cname, cvalue, cstatus, cssname, ccommand){
    var radioInput;
    try {
        var radioHtml = '<input type="radio" id="' + cid + '" name="' + cname + '" value=" ' + cvalue + ' "';
        if ( cstatus ) radioHtml += ' checked="' + cstatus + '"';
        if ( cssname ) radioHtml += ' class="' + cssname + '"';
        if ( ccommand ) radioHtml += ' onclick="' + ccommand + '"';
        radioHtml += '/>';
        radioInput = document.createElement(radioHtml);
		return radioInput;

	} catch( err ) {
		var inObj =document.createElement("INPUT");
		inObj.id =cid;
		inObj.name =cname;
		inObj.type ="radio";
		inObj.value =cvalue;
		inObj.checked =cstatus;
		inObj.className =cssname;
		inObj.onclick=function(){  eval(ccommand); }
		return inObj;
    }
}

function createBR(){
	var inObj =document.createElement("BR");
	inObj.className ='clear';
	return inObj;
}

// JavaScript Document
// new method for padding string
String.prototype.pad = function(l, s, t){
	return s || (s = " "), (l -= this.length) > 0 ? (s = new Array(Math.ceil(l / s.length)
		+ 1).join(s)).substr(0, t = !t ? l : t == 1 ? 0 : Math.ceil(l / 2))
		+ this + s.substr(0, l - t) : this;
};

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;
};

function floor_format(number, prec){
	var n =number;
	var p =Math.pow(10, prec);
	var e =n.toString();
	var r =e.split(".");
	if(r.length > 1){
		if(r[1] < p){
			return number_format(((n*p)/p), prec, ".", "");		
		}else{
			return number_format((Math.floor(n*p)/p), prec, ".", "");		
		}
	}else{
		return number_format((Math.floor(n*p)/p), prec, ".", "");	
	}
}

function ceil_format (number, prec){
	var n =number;
	var p =Math.pow(10, prec);
	var e =(n*p).toString();
    var r = e.split(".");
	if(r.length > 1){	
		var ro =r[1].substr(0, prec);
		if(parseInt(ro) > 0){
			return Math.ceil(n*p)/p;
			
		}else{
			return (n*p)/p;
		}
	}else{
		return (n*p)/p;
	}
}

function number_format (number, decimals, dec_point, thousands_sep) {
	var n = number, prec = decimals;
    
	var toFixedFix = function (n,prec) {
        var k = Math.pow(10,prec);
		return (Math.round(n*k)/k).toString();
    };
	
    n = !isFinite(+n) ? 0 : +n;
    prec = !isFinite(+prec) ? 2 : Math.abs(prec);
    var sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep;
    var dec = (typeof dec_point === 'undefined') ? '.' : dec_point;

	// is float
	if(Math.ceil(n)==n){
		prec =0;
		dec ="";
	}
	
    var s = (prec > 0) ? toFixedFix(n, prec) : toFixedFix(Math.round(n), prec); //fix for IE parseFloat(0.55).toFixed(0) = 0;

    var abs = toFixedFix(Math.abs(n), prec);
    var _, i;

    if (abs >= 1000) {
        _ = abs.split(/\D/);
        i = _[0].length % 3 || 3;

        _[0] = s.slice(0,i + (n < 0)) +
              _[0].slice(i).replace(/(\d{3})/g, sep+'$1');
        s = _.join(dec);
    } else {
        s = s.replace('.', dec);
    }

    var decPos = s.indexOf(dec);
    if (prec >= 1 && decPos !== -1 && (s.length-decPos-1) < prec) {
        s += new Array(prec-(s.length-decPos-1)).join(0)+'0';
    }
    else if (prec >= 1 && decPos === -1) {
        s += dec+new Array(prec).join(0)+'0';
    }
    return s;
}

function sprintf()
{
	if (!arguments || arguments.length < 1 || !RegExp)
	{
		return;
	}
	var str = arguments[0];
	var re = /([^%]*)%('.|0|\x20)?(-)?(\d+)?(\.\d+)?(%|b|c|d|u|f|o|s|x|X)(.*)/;
	var a = b = [], numSubstitutions = 0, numMatches = 0;
	while (a = re.exec(str))
	{
		var leftpart = a[1], pPad = a[2], pJustify = a[3], pMinLength = a[4];
		var pPrecision = a[5], pType = a[6], rightPart = a[7];
		
		//alert(a + '\n' + [a[0], leftpart, pPad, pJustify, pMinLength, pPrecision);

		numMatches++;
		if (pType == '%')
		{
			subst = '%';
		}
		else
		{
			numSubstitutions++;
			if (numSubstitutions >= arguments.length)
			{
				alert('Error! Not enough function arguments (' + (arguments.length - 1) + ', excluding the string)\nfor the number of substitution parameters in string (' + numSubstitutions + ' so far).');
			}
			var param = arguments[numSubstitutions];
			var pad = '';
				   if (pPad && pPad.substr(0,1) == "'") pad = leftpart.substr(1,1);
			  else if (pPad) pad = pPad;
			var justifyRight = true;
				   if (pJustify && pJustify === "-") justifyRight = false;
			var minLength = -1;
				   if (pMinLength) minLength = parseInt(pMinLength);
			var precision = -1;
				   if (pPrecision && pType == 'f') precision = parseInt(pPrecision.substring(1));
			var subst = param;
				   if (pType == 'b') subst = parseInt(param).toString(2);
			  else if (pType == 'c') subst = String.fromCharCode(parseInt(param));
			  else if (pType == 'd') subst = parseInt(param) ? parseInt(param) : 0;
			  else if (pType == 'u') subst = Math.abs(param);
			  else if (pType == 'f') subst = (precision > -1) ? Math.round(parseFloat(param) * Math.pow(10, precision)) / Math.pow(10, precision): parseFloat(param);
			  else if (pType == 'o') subst = parseInt(param).toString(8);
			  else if (pType == 's') subst = param;
			  else if (pType == 'x') subst = ('' + parseInt(param).toString(16)).toLowerCase();
			  else if (pType == 'X') subst = ('' + parseInt(param).toString(16)).toUpperCase();
		}
		str = leftpart + subst + rightPart;
	}
	return str;
}

