/*
	2006.07.21	gricke
				<typeof(entered.value)> Add debuging - error was throwing if 2 form fields had the same name
*/
function GenGUID(){
	var GUID = '';
	var GUIDTime = '';
	var GUIDRand = '';
	dateObj = new Date()
	//miliseconds since 1/1/70;
	GUIDTime = dateObj.getTime(6/17/1966);
	GUIDTime = GUIDTime.toString();
	GUIDRand = randomNum(); 
	GUIDRand = GUIDRand.toString();
	GUID = GUIDTime + GUIDRand;
	return GUID;
}

function randomNum(){
	var ran_unrounded=Math.random()*10000000000000000;
	var ran_number=Math.round(ran_unrounded); 
	return ran_number;
}

function remove_XS_whitespace(item){
  var tmp = "";
  var item_length = item.length;
  var item_length_minus_1 = item.length - 1;
  for (index = 0; index < item_length; index++)
  {
    if (item.charAt(index) != ' ')
    {
      tmp += item.charAt(index);
    }
    else
    {
      if (tmp.length > 0)
      {
        if (item.charAt(index+1) != ' ' && index != item_length_minus_1)
        {
          tmp += item.charAt(index);
        }
      }
    }
  }
  return tmp;
}

Number.prototype.toDecimals=function(n){
    n=(isNaN(n))?
        2:
        n;
    var
        nT=Math.pow(10,n);
    function pad(s){
            s=s||'.';
            return (s.length>n)?
                s:
                pad(s+'0');
    }
    return (isNaN(this))?
        this:
        (new String(
            Math.round(this*nT)/nT
        )).replace(/(\.\d*)?$/,pad);
}

function setCookie(name, value, expires, path, domain, secure) {
  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
 	  document.cookie = curCookie;
}

function getCookie(name) {
  var dc = document.cookie;
  var prefix = name + "=";
  var begin = dc.indexOf("; " + prefix);
  if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return null;
  } else
    begin += 2;
  var end = document.cookie.indexOf(";", begin);
  if (end == -1)
    end = dc.length;
  return unescape(dc.substring(begin + prefix.length, end));
}

function setCookieExpiration(DeleteKookie){
	var now = new Date();
	if(DeleteKookie == 'DeleteKookie'){
		//set 1 year previous to now
		now.setTime(now.getTime() - 1000 * 60 * 60 * 24 * 365);
		return now;
	}else{
		//set 1 year from now
		now.setTime(now.getTime() + 1000 * 60 * 60 * 24 * 365);
		return now;
	}
}

function SetGetMemberKookie(KookieName,KookieVal){
	var KookieSet = getCookie(KookieName);
	var MemberKookie = '';
	if(KookieSet == null){
		setCookie(KookieName,KookieVal,setCookieExpiration(''));
		MemberKookie = getCookie(KookieName);
	}else{
		setCookie(KookieName,KookieVal,setCookieExpiration(''));
		MemberKookie = getCookie(KookieName);
	}
	return MemberKookie;
}

function formatCurrency(num) {
	var isNegative = false;
	if (num < 0) {
		isNegative = true;
		num = num * -1;
	}
	
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num)) num = "0";
	cents = Math.floor((num*100+0.5)%100);
	num = Math.floor((num*100+0.5)/100).toString();
	if(cents < 10) cents = "0" + cents;
	for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
	num = num.substring(0,num.length-(4*i+3))+num.substring(num.length-(4*i+3));
	
	if (isNegative == true) {
		num = "-" + num;
	}
	
	return (num + '.' + cents);
}

function getQueryVariable(variable) {
  var query = window.location.search.substring(1);
  var vars = query.split("&");
  for (var i=0;i<vars.length;i++) {
    var pair = vars[i].split("=");
    if (pair[0] == variable) {
      return pair[1];
    }
  } 
  return '';
  //alert('Query Variable ' + variable + ' not found');
}

function URLDecode(StringEncode)
{
   // Replace + with ' '
   // Replace %xx with equivalent character
   // Put [ERROR] in output if %xx is invalid.
   var HEXCHARS = "0123456789ABCDEFabcdef"; 
   var encoded = StringEncode;
   var plaintext = "";
   var i = 0;
   while (i < encoded.length) {
       var ch = encoded.charAt(i);
	   if (ch == "+") {
	       plaintext += " ";
		   i++;
	   } else if (ch == "%") {
			if (i < (encoded.length-2) 
					&& HEXCHARS.indexOf(encoded.charAt(i+1)) != -1 
					&& HEXCHARS.indexOf(encoded.charAt(i+2)) != -1 ) {
				plaintext += unescape( encoded.substr(i,3) );
				i += 3;
			} else {
				alert( 'Bad escape combination near ...' + encoded.substr(i) );
				plaintext += "%[ERROR]";
				i++;
			}
		} else {
		   plaintext += ch;
		   i++;
		}
	} // while
   return plaintext;
};

function URLEncode(StringRaw)
{
	// The Javascript escape and unescape functions do not correspond
	// with what browsers actually do...
	var SAFECHARS = "0123456789" +					// Numeric
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
					"abcdefghijklmnopqrstuvwxyz" +
					"-_.!~*'()";					// RFC2396 Mark characters
	var HEX = "0123456789ABCDEF";

	var plaintext = StringRaw;
	var encoded = "";
	for (var i = 0; i < plaintext.length; i++ ) {
		var ch = plaintext.charAt(i);
	    if (ch == " ") {
		    encoded += "+";				// x-www-urlencoded, rather than %20
		} else if (SAFECHARS.indexOf(ch) != -1) {
		    encoded += ch;
		} else {
		    var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
			    alert( "Unicode Character '" 
                        + ch 
                        + "' cannot be encoded using standard URL encoding.\n" +
				          "(URL encoding only supports 8-bit characters.)\n" +
						  "A space (+) will be substituted." );
				encoded += "+";
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	} // for
	return encoded;
};

function emailvalidation(entered, alertbox)
{
// E-MAIL
with (entered)
	{
	apos=value.indexOf("@");
	dotpos=value.lastIndexOf(".");
	lastpos=value.length-1;
	if (apos<1 || dotpos-apos<2 || lastpos-dotpos>3 || lastpos-dotpos<2) 
	{if (alertbox) {alert(alertbox);} return false;}
	else {return true;}
	}
}
function phoneValidation(entered, alertbox){
	rePhoneNumber = new RegExp(/^\([1-9]\d{2}\)\s?\d{3}\-\d{4}$/);	//(555) 555-1234
	rePhoneNumber2 = new RegExp(/^[1-9]\d{2}\-\s?\d{3}\-\d{4}$/);	//555-555-1234
	rePhoneNumber3 = new RegExp(/^[1-9]\d{2}\.\s?\d{3}\.\d{4}$/);	//555.555.1234
	
    if (!rePhoneNumber.test(entered.value) && !rePhoneNumber2.test(entered.value) && !rePhoneNumber3.test(entered.value)) {
         alert(alertbox);
         return false;
    }else{
 		return true;
	}
}
function valuevalidation(entered, min, max, alertbox, datatype)
{
// VALUES
with (entered)
	{
	checkvalue=parseFloat(value);
	if (datatype)
	  {smalldatatype=datatype.toLowerCase();
	   if (smalldatatype.charAt(0)=="i") {checkvalue=parseInt(value)};
	  }
	if ((parseFloat(min)==min && checkvalue<min) || (parseFloat(max)==max && checkvalue>max) || value!=checkvalue)
	{if (alertbox!="") {alert(alertbox);} return false;}
	else {return true;}
	}
}

function digitvalidation(entered, min, max, alertbox, datatype)
{
// DIGITS
with (entered)
	{
	checkvalue=parseFloat(value);
	if (datatype)
	  {smalldatatype=datatype.toLowerCase();
	   if (smalldatatype.charAt(0)=="i") {checkvalue=parseInt(value); if (value.indexOf(".")!=-1) {checkvalue=checkvalue+1}};
	  }
	if ((parseFloat(min)==min && value.length<min) || (parseFloat(max)==max && value.length>max) || value!=checkvalue)
	{if (alertbox!="") {alert(alertbox);} return false;}
	else {return true;}
	}
}

function emptyvalidation(entered, alertbox)
{
	if(typeof(entered.value) == 'undefined'){
		alert('FORM ERROR \r\r >>>>' + alertbox + '<<<< has no value \r\rMake sure 2 form elements do not share the same name.');
		return false;
	}else{

		with (entered)
			{
			if (value==null || value=="")
				{
				if (alertbox!="") 
					{
					alert(alertbox);
					} 
					return false;
				}
			else {return true;}
			}
	}		
}
function emptyRadioValidation(entered, alertbox)
{
	if(typeof(entered[1].value) == 'undefined'){
		alert('FORM ERROR \r\r >>>>' + alertbox + '<<<< has no value \r\rMake sure 2 form elements do not share the same name.');
		return false;
	}else{
		var radioSelected = false;
		for(z=0;z<=(entered.length-1);z=z+1){
			if(entered[z].checked == true){
				radioSelected = true;
				break
			}
		}
		
		if(radioSelected == false){
			if(alertbox!=""){
				alert(alertbox);
			}
			return false;
		}else{
			return true;
		}
	}		
}
