function $(id){
	return document.getElementById(id);
}


/**
*
*  UTF-8 data encode / decode
*  http://www.webtoolkit.info/
*
**/
/////////////////////////////////////////////////////////////////
var keyStr = "ABCDEFGHIJKLMNOP" +
                "QRSTUVWXYZabcdef" +
                "ghijklmnopqrstuv" +
                "wxyz0123456789+/" +
                "=";

   function encode64(input) {
      var output = "";
      var chr1, chr2, chr3 = "";
      var enc1, enc2, enc3, enc4 = "";
      var i = 0;

      do {
         chr1 = input.charCodeAt(i++);
         chr2 = input.charCodeAt(i++);
         chr3 = input.charCodeAt(i++);

         enc1 = chr1 >> 2;
         enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
         enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
         enc4 = chr3 & 63;

         if (isNaN(chr2)) {
            enc3 = enc4 = 64;
         } else if (isNaN(chr3)) {
            enc4 = 64;
         }

         output = output +
            keyStr.charAt(enc1) +
            keyStr.charAt(enc2) +
            keyStr.charAt(enc3) +
            keyStr.charAt(enc4);
         chr1 = chr2 = chr3 = "";
         enc1 = enc2 = enc3 = enc4 = "";
      } while (i < input.length);

      return output;
   }
   
////////////////////////////////////////////////////////////////////

   function decode64(input) {
      var output = "";
      var chr1, chr2, chr3 = "";
      var enc1, enc2, enc3, enc4 = "";
      var i = 0;

      // remove all characters that are not A-Z, a-z, 0-9, +, /, or =
      var base64test = /[^A-Za-z0-9\+\/\=]/g;
      if (base64test.exec(input)) {
         return false;
      }
      input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

      do {
         enc1 = keyStr.indexOf(input.charAt(i++));
         enc2 = keyStr.indexOf(input.charAt(i++));
         enc3 = keyStr.indexOf(input.charAt(i++));
         enc4 = keyStr.indexOf(input.charAt(i++));

         chr1 = (enc1 << 2) | (enc2 >> 4);
         chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
         chr3 = ((enc3 & 3) << 6) | enc4;

         output = output + String.fromCharCode(chr1);

         if (enc3 != 64) {
            output = output + String.fromCharCode(chr2);
         }
         if (enc4 != 64) {
            output = output + String.fromCharCode(chr3);
         }

         chr1 = chr2 = chr3 = "";
         enc1 = enc2 = enc3 = enc4 = "";

      } while (i < input.length);

      return output;
   }
//////////////////////////////////////////////////////////////////////////

function confirmSubmit(lg,txt,formId){
	if(!txt){
			txt="Are you sure you want to continue?";
		}
		var answer = confirm(txt);
		if(answer){
			if(formId){
			$(formId).submit();}
			else {
			return true;}}
		else{ return false;}
}

function changeCSS(obj, bgColor, bdColor, ftColor) {
   	if (document.getElementById) {
		obj.style.backgroundColor = bgColor;
		obj.style.borderColor = bdColor;
		obj.style.color = ftColor; }
}

//Show Popup Embedded Dialogue Boz
var dialogue_num = 0;
function popupDialogue(popupID, innerhtml, location, bgColor, position, positionMove, OK_function, NO_function){
	var box='';
	var top='';
	var bottom='';
	var align='';
	var OK, NO;
	if(OK_function){
		OK = decode64(OK_function);
	}
	if(NO_function){
		NO = decode64(NO_function);
	}
	if(!popupID){
		popupID = "dialogue"+dialogue_num;
		dialogue_num++;
	}
	if(bgColor==""){
		bgColor = "EFEFEF";}
		
	position = position.split(":");
		
	if(position[0]=="top"){
		top='<img style="border:0px" src="images/dialoguearrow_bottom.gif" />';
		if(!positionMove){
		positionMove='bottom:200px;';}
	}
	else{
		bottom='<img style="border:0px" src="images/dialoguearrow_top.gif" />';
	}
	
	if(position[1]=="left"){
		align = "right";}
	if(position[1]=="right"){
		align = "left";
	}
	
	if(decode64(innerhtml)){
	innerhtml = decode64(innerhtml);}
	
	box= '<div id="'+popupID+'" style="position:absolute;display:inline;'+positionMove+'"><div style="margin:5px;cursor:default;">';
	if(bottom!=''){
	box+='<div align="'+align+'">'+bottom+'</div>';}
    box+='<div style="width:200px; border:5px solid #CCCCCC; background-color:#'+bgColor+'; padding:5px;">'+innerhtml;
    box+='<div style="margin-top:3px; padding:3px; border-top:1px dashed #CCCCCC">';
    box+='<input onclick="'+OK+'; $(\''+popupID+'\').parentNode.removeChild($(\''+popupID+'\'));" class="bluebutton" type="button" value="OK"/> ';
	if(OK_function){
    box+='<input class="bluebutton" type="button" value="No, thanks" onclick="$(\''+popupID+'\').parentNode.removeChild($(\''+popupID+'\')); '+NO+'" />';
	}
	box+='</div>';
	box+='</div>';
	if(top!=''){
	box+='<div align="'+align+'">'+top+'</div>';}
	box+='</div>';
	$(location).innerHTML = box;
}

//Show Notification			
function showNotification(msg, skin, location,elid, width, size,useid) {
	if(!$(id)) {
		var id = "";
		if(useid) {
			id = elid+"["+useid+"]";}
			else {
				id= elid; }
 $(location).innerHTML='<div style="margin:10px" id="'+id+'" align="center"><span style="width:'+width+'; font-size:'+size+';" class="notify'+skin+'"><div style="display:inline; width:95%">'+msg+'</div><div align="right" style="display:inline; width:5%; border-left:1px solid #CCCCCC"><img src="'+rootdir+'/html/images/no.png" width="12px" onclick="hideNotification(\''+location+'\');" style="cursor:pointer;" /></div></span></div>';
}
var settimeout = "hideNotification('"+location+"')"; 			
setTimeout(settimeout, 5*1000);
return false;
}

function hideNotification(location) {
	if($(location)){
	$(location).innerHTML = ""; }
}
function hideObject(obj){
	$(obj).style.display="none";}
	
//Show Loading Image
var topLoadingID = 0;
function showtopLoading(msg, skin, location, id){
	var output="";
	if(!id){
			id = "topLoadingID["+topLoadingID+"]";
			topLoadingID++;
	}
	if(skin=="sm") {
		output+= '<div align="center" style="right:45%; top:0px; background-color:#F0FFFF" id="loadingtopp"  id="'+id+'">';
		output+= '<span style="color:#333333; vertical-align:bottom; background-color:#F0FFFF; font-size:13px; padding:10px 10px 5px 10px; font-family:\'Trebuchet MS\';">'+msg+'</span>';
		output+= '</div>';
	}
	if(skin=="la") {
		$(location).innerHTML = "<div id=\""+id+"\" width=\"450px\" cellspacing=\"0\" cellpadding=\"0\"><div>"+msg+"</div><div><img src=\"images/loading.gif\"/></div></div>"+$(location).innerHTML; 
	}
	$(location).innerHTML+=output;
}
var loadingID = 0;
function showLoading(msg, skin, location, id, timeout) {
	if(!id){
			id = "LoadingID["+loadingID+"]";
			loadingID++;
	}
	if(skin=="sm") {
		$(location).innerHTML = '<div style="display:inline" id="'+id+'"><img style="vertical-align:middle" src="images/working.gif" /><span style="color:#333333; font-family:"Trebuchet MS";">  '+msg+'</span></div>';
	}
	if(skin=="la") {
		$(location).innerHTML = "<div id=\""+id+"\" width=\"450px\" cellspacing=\"0\" cellpadding=\"0\"><div>"+msg+"</div><div><img src=\"images/loading.gif\"/></div></div>"+$(location).innerHTML; 
	}
	if(timeout){
		setTimeout(function(e){hideLoading(id);},timeout);}
}

function hideLoading(location) {
	if($(location)){
	$(location).parentNode.removeChild($(location));} }
	
function removeElement(location) {
	if($(location)){
	$(location).parentNode.removeChild($(location));} }


/*
 
Correctly handle PNG transparency in Win IE 5.5 & 6.
http://homepage.ntlworld.com/bobosola. Updated 18-Jan-2006.

Use in <HEAD> with DEFER keyword wrapped in conditional comments:
<!--[if lt IE 7]>
<script defer type="text/javascript" src="pngfix.js"></script>
<![endif]-->

*/
// PNG IE-6 Transparency
var arVersion = navigator.appVersion.split("MSIE")
var version = parseFloat(arVersion[1])

if (5.5<version<7) {
   for(var i=0; i<document.images.length; i++)
   {
      var img = document.images[i]
      var imgName = img.src.toUpperCase()
      if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
      {
         var imgID = (img.id) ? "id='" + img.id + "' " : ""
         var imgClass = (img.className) ? "class='" + img.className + "' " : ""
         var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
         var imgStyle = "display:inline-block;" + img.style.cssText 
         if (img.align == "left") imgStyle = "float:left;" + imgStyle
         if (img.align == "right") imgStyle = "float:right;" + imgStyle
         if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
         var strNewHTML = "<span " + imgID + imgClass + imgTitle
         + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
         + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
         + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>" 
         img.outerHTML = strNewHTML
         i = i-1
      }
   }
}




//AJAX Load new Page without reloading
function open_url(url, target, loading_txt, loading_skin, loading_target, loading_id) {
 	if ( ! document.getElementById) {
  		return false;
 	}

 	if (window.ActiveXObject) {
  		link = new ActiveXObject("Microsoft.XMLHTTP");
 	} else if (window.XMLHttpRequest) {
  		link = new XMLHttpRequest();
 	}

 	if (link == undefined) {
  		return false;
 	}
 	link.onreadystatechange = function() { response(url, target,loading_txt, loading_skin, loading_target, loading_id); }
 	link.open("GET", url, true);
 	link.send(null);
}


function response(url, target,loading_txt, loading_skin, loading_target, loading_id) {
	var new_loading_txt, new_loading_skin, new_loading_target, new_loading_id;
	if(loading_txt || loading_skin || loading_target || loading_id){
			if(loading_txt=="default"){
				new_loading_text = "Working on your request...";}
				else{
				new_loading_text = loading_txt;}
			if(!loading_skin || loading_skin=="default"){
				new_loading_skin = "sm";}
				else{
				new_loading_skin = loading_skin;}
			if(!loading_target || loading_target=="default"){
				new_loading_target = target;}
				else{
				new_loading_target = loading_target;}
			if(!loading_id || loading_id=="default"){
				var loading_id_default = "loadingpage";
				var i = 0;
				var generated_id = null;
				
				while(loading_id_default && !generated_id){
					
					if(!$(loading_id_default+"["+i+"]")){
						generated_id = loading_id_default+"["+i+"]";
					}
					i++;
				}
			}
				else{
				generated_id = loading_id;}
	}
	if(link.readystate!=4) {
		if(loading_txt || loading_skin || loading_target || loading_id){
				if($(new_loading_target).innerHTML==""){
			showLoading(new_loading_text,new_loading_skin,new_loading_target,generated_id,"10000");}
		}
		else{
		showtopLoading("Working on your request...","sm",target,"loadingpage");
		}}
 	if (link.readyState == 4) {
	 	$(target).innerHTML = (link.status == 200) ? link.responseText : "Ooops!! A broken link! Please contact the webmaster of this website ASAP and give him the following errorcode: " + link.status;
		 if(new_loading_target){
			 $(new_loading_target).innerHTML="";}
		 }
}


// Validation Functions:
function checkEmail (strng) {
var error="";
if (strng == "") {
   error = "-You didn't enter an email address.\n";
}

    var emailFilter=/^.+@.+\..{2,3}$/;
    if (!(emailFilter.test(strng))) { 
       error = "-Please enter a valid email address.\n";
    }
    else {
//test email for illegal characters
       var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/
         if (strng.match(illegalChars)) {
          error = "-The email address contains illegal characters.\n";
       }
    }
return error;    
}


// phone number - strip out delimiters and check for 10 digits

function checkPhone (strng) {
var error = "";
if (strng == "") {
   error = "-You didn't enter a phone number.\n";
}

var stripped = strng.replace(/[\(\)\.\-\ ]/g, ''); //strip out acceptable non-numeric characters
    if (isNaN(parseInt(stripped))) {
       error = "-The phone number contains illegal characters.";
  
    }
    if (!(stripped.length == 10)) {
	error = "-The phone number is the wrong length. Make sure you included an area code.\n";
    } 
return error;
}


// password - between 6-8 chars, uppercase, lowercase, and numeral

function checkPassword (strng) {
var error = "";
if (strng == "") {
   error = "-You didn't enter a password.\n";
}

    var illegalChars = /[\W_]/; // allow only letters and numbers
    
    if ((strng.length < 6) || (strng.length > 8)) {
       error = "-The password is the wrong length.\n";
    }
    else if (illegalChars.test(strng)) {
      error = "-The password contains illegal characters.\n";
    } 
    else if (!((strng.search(/(a-z)+/)) && (strng.search(/(A-Z)+/)) && (strng.search(/(0-9)+/)))) {
       error = "-The password must contain at least one uppercase letter, one lowercase letter, and one numeral.\n";
    }  
return error;    
}    


// username - 4-10 chars, uc, lc, and underscore only.

function checkUsername (strng) {
var error = "";
if (strng == "") {
   error = "-You didn't enter a username.\n";
}


    var illegalChars = /\W/; // allow letters, numbers, and underscores
    if ((strng.length < 4) || (strng.length > 10)) {
       error = "-The username is the wrong length.\n";
    }
    else if (illegalChars.test(strng)) {
    error = "-The username contains illegal characters.\n";
    } 
return error;
}       


// non-empty textbox

function isEmpty(strng) {
var error = "";
  if (strng.length == 0) {
     error = "-The required text has not been filled in.\n"
  }
return error;	  
}

// was textbox altered

function isDifferent(strng) {
var error = ""; 
  if (strng != "Can\'t touch this!") {
     error = "-You altered the inviolate text area.\n";
  }
return error;
}

// exactly one radio button is chosen

function checkRadio(checkvalue) {
var error = "";
   if (!(checkvalue)) {
       error = "-Required radio buttons have not been checked.\n";
    }
return error;
}

// valid selector from dropdown list

function checkDropdown(choice) {
var error = "";
    if (choice == 0) {
    error = "-You didn't choose an option from the drop-down list(s).\n";
    }    
return error;
}    

