// Global user functions


//***********CHECKING*******************


// Check if object has value
function ew_HasValue(obj) {
	if (!obj)
		return true;
	var type = (!obj.type && obj[0]) ? obj[0].type : obj.type;
	if (type == "text" || type == "password" || type == "textarea" ||
		type == "file" || type == "hidden") {
		return (obj.value.length != 0);
	} else if (type == "select-one") {
		return (obj.selectedIndex > 0);
	} else if (type == "select-multiple") {
		return (obj.selectedIndex > -1);
	} else if (type == "checkbox") {
		if (obj[0]) {
			for (var i=0; i < obj.length; i++) {
				if (obj[i].value != "{value}" && obj[i].checked)
				return true;
			}
			return false;
		}
	} else if (type == "radio") {
		if (obj[0]) {
			for (var i=0; i < obj.length; i++) {
				if (obj[i].value != "{value}" && obj[i].checked)
				return true;
			}
			return false;
		} else {
			return (obj.value != "{value}" && obj.checked);
		}
	}
	return true;
}


// Show error message
function ew_OnError(page, input_object, error_message) {
	alert(error_message); 
	if (page && page.MultiPage) // check if multi-page
		page.MultiPage.GotoPageByElement(input_object);
	ew_SetFocus(input_object);
	return false;
}






// Check email
function ew_CheckEmail(object_value) {
	if (object_value == null)
		return true;
	if (object_value.length == 0)
		return true;
	object_value = object_value.replace(/^\s*|\s*$/g, "");
	var re = new RegExp("^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,6}$", "i");
	return re.test(object_value);
}


//

// Check Date format (yyyy/mm/dd)
function ew_CheckDate(object_value) {
	if (object_value == null)
		return true;
	if (object_value.length == 0)
		return true;
	isplit = object_value.indexOf(EW_DATE_SEPARATOR); // Split by date separator
	if (isplit == -1 || isplit == object_value.length)
		return false;
	sYear = object_value.substring(0, isplit);
	isplit = object_value.indexOf(EW_DATE_SEPARATOR, isplit + 1); // Split by date separator
	if (isplit == -1 || (isplit + 1 ) == object_value.length)
		return false;
	sMonth = object_value.substring((sYear.length + 1), isplit);
	if (sMonth.length == 0)
		return false;
	isep = object_value.indexOf(' ', isplit + 1); 
	if (isep == -1) {
		sDay = object_value.substring(isplit + 1);
	} else {
		sDay = object_value.substring(isplit + 1, isep);
		sTime = object_value.substring(isep + 1);
		if (!ew_CheckTime(sTime)) // Check time
			return false; 
	}
	if (sDay.length == 0)
		return false;
	if (!ew_CheckInteger(sMonth))
		return false;
	else if (!ew_CheckRange(sMonth, 1, 12))
		return false;
	else if (!ew_CheckInteger(sYear))
		return false;
	else if (!ew_CheckRange(sYear, 0, 9999))
		return false;
	else if (!ew_CheckInteger(sDay))
		return false;
	else if (!ew_CheckDay(sYear, sMonth, sDay))
		return false;
	else
		return true;
}

// Check integer
function ew_CheckInteger(object_value) {
	if (object_value == null)
		return true;
	if (object_value.length == 0)
		return true;
	var decimal_format = ".";
	var check_char;
	check_char = object_value.indexOf(decimal_format);
	if (check_char < 1)
		return ew_CheckNumber(object_value);
	else
		return false;
}


// Check time
function ew_CheckTime(object_value) {
	if (object_value == null)
		return true;
	if (object_value.length == 0)
		return true;
	var time_value;
	isplit = object_value.indexOf('.');
	if (isplit > 0)
		time_value = object_value.substring(0, isplit);
	else
		time_value = object_value;
	isplit = time_value.indexOf(':');
	if (isplit == -1 || isplit == time_value.length)
		return false;
	sHour = time_value.substring(0, isplit);
	iminute = time_value.indexOf(':', isplit + 1);
	if (iminute == -1 || iminute == time_value.length)
		sMin = time_value.substring((sHour.length + 1));
	else
		sMin = time_value.substring((sHour.length + 1), iminute);
	if (!ew_CheckInteger(sHour))
		return false;
	else if (!ew_CheckRange(sHour, 0, 23))
		return false;
	if (!ew_CheckInteger(sMin))
		return false;
	else if (!ew_CheckRange(sMin, 0, 59))
		return false;
	if (iminute != -1) {
		sSec = time_value.substring(iminute + 1);		
		if (!ew_CheckInteger(sSec))
			return false;
		else if (!ew_CheckRange(sSec, 0, 59))
			return false;	
	}
	return true;
}

//************************************************************

function redirect(link)
{
 if (link=="")
   {alert("Not done!")
     return false;
   }
  window.location=link;
 }


//*************************
function getid(idx){
alert(idx);
}

//*************************

function ShowHideObject(objectid) {
	//alert(objectid);
	var ele = document.getElementById(objectid);
   // alert(document.getElementById('box_comment').style.display);
	if(ele.style.display == "block") {
    		ele.style.display = "none";
		//	createCookie(objectid,"none");

  	}
	else {
		ele.style.display = "block";
		//createCookie(objectid,"block");

	}
} 




//Show 1 object and hide other and revert
function ShowHideObject2(objectid1,objectid2) {


	var ele1 = document.getElementById(objectid1);
    var ele2 = document.getElementById(objectid2);

    //alert ("ele1.style.display="+ele1.style.display);
    //alert ("ele2.style.display="+ele2.style.display);

	if(ele1.style.display == "block") 
		{
    		ele1.style.display = "none";
			ele2.style.display = "block";
            //alert("not else");
     	}
	else {
		//alert("else");
		ele1.style.display = "block";
		ele2.style.display = "none";

	}

   // alert ("2ele1.style.display="+ele1.style.display);
   // alert ("2ele2.style.display="+ele2.style.display);
}



function StrReverse(txt) 
	// dao nguoc chuoi
	{
text = "";
str = txt
for (i = 0; i <= str.length; i++)
text = str.substring(i, i+1) + text;
return text
}





function GetSelectedItem(fobj) {
len = fobj.length
i = 0
chosen = ""
for (i = 0; i < len; i++) {
if (fobj[i].selected) {
chosen = chosen +"."+ trim(fobj[i].value) + ".,"
}
}
return chosen
} 
function Left(str, n){
	if (n <= 0)
	    return "";
	else if (n > String(str).length)
	    return str;
	else
	    return String(str).substring(0,n);
}
function Right(str, n){
    if (n <= 0)
       return "";
    else if (n > String(str).length)
       return str;
    else {
       var iLen = String(str).length;
       return String(str).substring(iLen, iLen - n);
    }
}
function trim(s)
{
	var l=0; var r=s.length -1;
	while(l < s.length && s[l] == ' ')
	{	l++; }
	while(r > l && s[r] == ' ')
	{	r-=1;	}
	return s.substring(l, r+1);
}
/*collapse expand single item
by Leo Charre & Jesse Fergusson
Internet Connection  2004 2005 ©
www.internetconnection.net
Usage:
place this in your HEAD tags:
	<script language="JavaScript" src="/WHEREINPATH/collapse_expand_single_item.js"></script>
Place this in your HTML
	<img src="/IMAGESDIR/u.gif" name="imgfirst" width="9" height="9" border="0" >
	<a  href="#first" onClick="shoh('first');" >Customer Support</a>
	<div style="display: none;" id="first" >
			With its friendly solutions-oriented 
			approach, our timely and knowledgeable Technical Support Staff are 
			completely at your disposal. Our Support Technicians are highly 
			trained on the inner workings of the Internet and its associated 
			technologies. Combined with effective troubleshooting techniques, 
			we can quickly identify and resolve technical issues whether they 
			are on our end or on the client end. 		      
	</div>
WHEREINPATH is where you are storing this script on your account
IMAGESDIR is where on your acoount you are storing the icons for (expanded collapsed)
*/
imgout=new Image(9,9);
imgin=new Image(9,9);
/////////////////BEGIN USER EDITABLE///////////////////////////////
	imgout.src="images/u.gif";
	imgin.src="images/d.gif";
///////////////END USER EDITABLE///////////////////////////////////
//this switches expand collapse icons
function filter(imagename,objectsrc){
	if (document.images){
		document.images[imagename].src=eval(objectsrc+".src");
	}
}
//show OR hide funtion depends on if element is shown or hidden
function shoh(id) { 
	if (document.getElementById) { // DOM3 = IE5, NS6
		if (document.getElementById(id).style.display == "none"){
			document.getElementById(id).style.display = 'block';
			filter(("img"+id),'imgin');			
		} else {
			filter(("img"+id),'imgout');
			document.getElementById(id).style.display = 'none';			
		}	
	} else { 
		if (document.layers) {	
			if (document.id.display == "none"){
				document.id.display = 'block';
				filter(("img"+id),'imgin');
			} else {
				filter(("img"+id),'imgout');	
				document.id.display = 'none';
			}
		} else {
			if (document.all.id.style.visibility == "none"){
				document.all.id.style.display = 'block';
			} else {
				filter(("img"+id),'imgout');
				document.all.id.style.display = 'none';
			}
		}
	}
}
function shmenu(id) { 
	if (document.getElementById) { // DOM3 = IE5, NS6
		if (document.getElementById(id).style.display == "none"){
			document.getElementById(id).style.display = 'block';
			document.getElementById("a_menu").value=1;
            document.getElementById("menu").style.backgroundColor ="#5B80CC"
			x1=saveIt("menu_status");
			filter(("img"+id),'imgin');			
		} else {
			filter(("img"+id),'imgout');
			document.getElementById(id).style.display = 'none';	
			document.getElementById("a_menu").value=0;
			document.getElementById("menu").style.backgroundColor ="#F7F7EF"
			x1=saveIt("menu_status");
		}	
	} else { 
		if (document.layers) {	
			if (document.id.display == "none"){
				document.id.display = 'block';
				document.a_menu.value=1
				document.menu.backgroundColor="#5B80CC"
					x1=saveIt("menu_status");
				filter(("img"+id),'imgin');
			} else {
				filter(("img"+id),'imgout');	
				document.id.display = 'none';
				document.menu.backgroundColor="#F7F7EF"
				x1=saveIt("menu_status");
				document.a_menu.value=0
			}
		} else {
			if (document.all.id.style.visibility == "none"){
				document.all.id.style.display = 'block';
				document.all.menu.backgroundColor="#5B80CC"
				document.all.a_menu.value=1
					x1=saveIt("menu_status");
			} else {
				filter(("img"+id),'imgout');
				document.all.id.style.display = 'none';
				document.all.a_menu.value=0
					document.all.menu.backgroundColor="#F7F7EF"
					x1=saveIt("menu_status");
			}
		}
	}
}
function saveIt(name) {
	var x = document.forms['frmmenu'].a_menu.value;
	if (!x)
	alert('Please fill in a value in the input box.');
	else {
		createCookie(name,x,7);
		//alert('Cookie created');
	}
}
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
		//alert(date.toGMTString());
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}
function 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 eraseCookie(name) {
	createCookie(name,"",-1);
	//tao bang asp thi xoa bang asp; tao bang javascript  thi xoa bang javascript
	
}
function browserCheck() {
	if (navigator.appName == 'Netscape'){
		var version = navigator.appVersion;
		version=version.substring(0,4); 
		if(version < '5.0')
		window.alert("You are using a browser that is either too old or not supported.\nPlease upgrade your browser to a required version.");
		else
		window.alert("You are using "+navigator.vendor+" "+navigator.vendorSub);
	}
	else if (navigator.appName == 'Microsoft Internet Explorer')	
	{
	var version = navigator.appVersion;
	version=version.substring(22,25); 
	if(version < '6.0')
	window.alert("You are using a browser that is either too old or not supported.\nPlease upgrade your browser to a required version."+navigator.appName+" "+version);
	else
	window.alert("You are using "+navigator.appName+" "+version);
	}
	else 
	window.alert("You are using a browser that is either too old or not supported.\nPlease upgrade your browser to a required version."+navigator);
}
function doJavaScriptTest() {
    window.alert( "JavaScript is enabled!" );
  }

  function doCookieTest() {
    var msg;
    if ( navigator.cookieEnabled ) {
     // msg = "Cookies are enabled on this browser! ";
    } else {
      msg = "Cookies are not enabled on this browser! ";
      msg += "Please enable cookies in your browser in order to login to ITDR. You will not be able to access systems requiring authentication if your cookies are disabled.";
	   window.alert( msg );
    }
   
  }


function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

// set the radio button with the given value as being checked
// do nothing if there are no radio buttons
// if the given value does not exist, all the radio buttons
// are reset to unchecked
function setCheckedValue(radioObj, newValue) {
	if(!radioObj)
		return;
	var radioLength = radioObj.length;
	if(radioLength == undefined) {
		radioObj.checked = (radioObj.value == newValue.toString());
		return;
	}
	for(var i = 0; i < radioLength; i++) {
		radioObj[i].checked = false;
		if(radioObj[i].value == newValue.toString()) {
			radioObj[i].checked = true;
		}
	}
}




//Use : alert("encode="+ Url.encode(x2));
//		alert("decode="+Url.decode(Url.encode(x2)));

var Url = {

 	// public method for URL encoding
 	encode : function (string) {
 		 return escape(this._utf8_encode(string));
 	},

 	// public method for URL decoding
	 decode : function (string) {
 	 	return this._utf8_decode(unescape(string));
 	},

 	// private method for UTF-8 encoding
 	_utf8_encode : function (string) {
  		string = string.replace(/\r\n/g,"\n");
 	 	var utftext = "";

  		for (var n = 0; n < string.length; n++) {
   			var c = string.charCodeAt(n);
   			if (c < 128) {
    				utftext += String.fromCharCode(c);
 			} else if((c > 127) && (c < 2048)) {
  				utftext += String.fromCharCode((c >> 6) | 192);
  				utftext += String.fromCharCode((c & 63) | 128);
 			} else {
  				utftext += String.fromCharCode((c >> 12) | 224);
  				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
 	 			utftext += String.fromCharCode((c & 63) | 128);
 			}
 	}

		return utftext;
	},

 	// private method for UTF-8 decoding
 	_utf8_decode : function (utftext) {
 		 var string = "";
 		 var i = 0;
 		 var c = c1 = c2 = 0;

  		while ( i < utftext.length ) {
  			 c = utftext.charCodeAt(i);
   			if (c < 128) {
    				string += String.fromCharCode(c);
    				i++;
  			 } else if((c > 191) && (c < 224)) {
 				   c2 = utftext.charCodeAt(i+1);
    				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
    				i += 2;
  			 } else {
 				   c2 = utftext.charCodeAt(i+1);
    				c3 = utftext.charCodeAt(i+2);
    				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
   				 i += 3;
 			  }
		  }
		return string;
	 }
}




