/** XHConn - Simple XMLHTTP Interface - bfults@gmail.com - 2005-04-08        **
 ** Code licensed under Creative Commons Attribution-ShareAlike License      **
 ** http://creativecommons.org/licenses/by-sa/2.0/                           **/
function XHConn()
{
  var xmlhttp, bComplete = false;
  try { xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }
  catch (e) { try { xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }
  catch (e) { try { xmlhttp = new XMLHttpRequest(); }
  catch (e) { xmlhttp = false; }}}
  if (!xmlhttp) return null;
  this.connect = function(sURL, sMethod, sVars, fnDone)
  {
    if (!xmlhttp) return false;
    bComplete = false;
    sMethod = sMethod.toUpperCase();

    try {
      if (sMethod == "GET")
      {
        xmlhttp.open(sMethod, sURL+"?"+sVars, true);
        sVars = "";
      }
      else
      {
        xmlhttp.open(sMethod, sURL, true);
        xmlhttp.setRequestHeader("Method", "POST "+sURL+" HTTP/1.1");
        xmlhttp.setRequestHeader("Content-Type",
          "application/x-www-form-urlencoded");
      }
      xmlhttp.onreadystatechange = function(){
        if (xmlhttp.readyState == 4 && !bComplete)
        {
          bComplete = true;
          fnDone(xmlhttp);
        }};
      xmlhttp.send(sVars);
    }
    catch(z) { return false; }
    return true;
  };
  return this;
}

function addSmiley(thisElement,smileycode) {
	document.getElementById(thisElement).value += " " + smileycode + " ";
	}

function limitText(limitField, limitCount, limitNum) {
	if (document.getElementById(limitField).value.length > limitNum) {
			document.getElementById(limitField).value = document.getElementById(limitField).value.substring(0, limitNum);
	} else {
		document.getElementById(limitCount).value = limitNum - document.getElementById(limitField).value.length;
	}
}

function checkUsername(thisAction){
	//ajax request to check if username is already registered, and return suggestion?
	document.getElementById("UsernameStatus").innerHTML = "checking database..."
	document.getElementById("UsernameStatus").style.display=''
	var thisUsername=document.getElementById("Username").value
	var url=parent.location.protocol + "//" + document.domain + "/remote/rem_check_username.asp"
	var myConn = new XHConn()
	if (!myConn) alert("XMLHTTP not available. Try a newer/better browser.")

	var fnWhenDone = function (oXML) {
		//alert(oXML.responseText)
		response = oXML.responseXML.documentElement
		if(response.getElementsByTagName("message")[0].firstChild!=null)
		{var thisMessage = response.getElementsByTagName("message")[0].firstChild.data}
		document.getElementById("UsernameStatus").innerHTML = thisMessage
		document.getElementById("UsernameStatus").style.display=''
		}
	myConn.connect(url, "POST","username=" + thisUsername + "&action=" + thisAction,fnWhenDone)
}

function showhide_div(div_id){
	// show the requested div
	//var lastMenu = GetCookie(div_id);
	if(document.getElementById(div_id))
		{
		if (document.getElementById(div_id).style.display == '')
			{
			//document.getElementById(div_id).style.display = 'none';
			//document.getElementById(div_id).style.visibility = 'hidden';
			Effect.SlideUp(div_id)
			if(document.getElementById("img_"+div_id))
				{
				document.getElementById("img_"+div_id).src="../images-main/more.gif";
				}
			SetCookie(div_id, 'none');
			}
		else
			{
			//document.getElementById(div_id).style.display = '';
			//document.getElementById(div_id).style.visibility = 'visible';
			Effect.SlideDown(div_id)
			if(document.getElementById("img_"+div_id)){
				document.getElementById("img_"+div_id).src="../images-main/less.gif";
			}
		SetCookie(div_id, '');
		}
	}
}

function showdiv(div_id){
	if(document.getElementById(div_id))
		{
		document.getElementById(div_id).style.display=""
		}
}

function hidediv(div_id){
	if(document.getElementById(div_id))
		{
		document.getElementById(div_id).style.display="none"
		}
}

function IsNumeric(sText) {
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;

   for (i = 0; i < sText.length && IsNumber == true; i++)
	  {
	  Char = sText.charAt(i);
	  if (ValidChars.indexOf(Char) == -1)
		 {
		 IsNumber = false;
		 }
	  }
   return IsNumber;
}

function GetCookie (name) {
  var arg = name + "=";
  var alen = arg.length;
  var clen = document.cookie.length;
  var i = 0;

  while (i < clen) {
    var j = i + alen;

   if (document.cookie.substring(i, j) == arg)
       return getCookieVal (j);

      i = document.cookie.indexOf(" ", i) + 1;

 if (i == 0) break;

}

return null;

}

function SetCookie (name, value) {
  var argv = SetCookie.arguments;
  var argc = SetCookie.arguments.length;
  var expires = (argc > 2) ? argv[2] : null;
  var path = (argc > 3) ? argv[3] : null;
  var domain = (argc > 4) ? argv[4] : null;
  var secure = (argc > 5) ? argv[5] : false;
document.cookie = name + "=" + escape (value) +
((expires == null) ? "" : ("; expires=" + expires.toGMTString())) +
((path == null) ? "" : ("; path=" + path)) +
((domain == null) ? "" : ("; domain=" + domain)) +
((secure == true) ? "; secure" : "");

}

function DeleteCookie (name) {
  var exp = new Date();
  exp.setTime (exp.getTime() - 1);
  var cval = GetCookie (name);
  document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString();

}


function amt(){
  var count = GetCookie('count');

  if(count == null) {
    SetCookie('count','1');
    return 1;
} else {
   var newcount = parseInt(count) + 1;
   DeleteCookie('count');
   SetCookie('count',newcount,exp);
return count

}

}

function getCookieVal(offset) {
   var endstr = document.cookie.indexOf (";", offset);
   if (endstr == -1)
     endstr = document.cookie.length;
     return unescape(document.cookie.substring(offset, endstr));
}

function checkCount() {
  var count = GetCookie('count');
  if (count == null) {
    count=1;
    SetCookie('count', count, exp);
    window.open(page, "", windowprops);
  } else {
    count++;
    SetCookie('count', count, exp);
  }

}

function replace(string,text,by) {
// Replaces text with by in string
    var strLength = string.length, txtLength = text.length;
    if ((strLength == 0) || (txtLength == 0)) return string;

    var i = string.indexOf(text);
    if ((!i) && (text != string.substring(0,txtLength))) return string;
    if (i == -1) return string;

    var newstr = string.substring(0,i) + by;

    if (i+txtLength < strLength)
        newstr += replace(string.substring(i+txtLength,strLength),text,by);

    return newstr;
}

function checkDate(strDate,strFormat){
    if(strDate.length>0){
    	var dateregex=/^[ ]*[0]?(\d{1,2})\/(\d{1,2})\/(\d{4,})[ ]*$/;

        var match=strDate.match(dateregex);
        if (match){
        	switch (strFormat) {
				case 'MDY':
					var tmpdate=new Date(match[3],parseInt(match[1],10)-1,match[2]);
					if (tmpdate.getDate()==parseInt(match[2],10) && tmpdate.getFullYear()==parseInt(match[3],10) && (tmpdate.getMonth()+1)==parseInt(match[1],10)){
						return true;
					}
					break;
				case 'DMY':
					var tmpdate=new Date(match[3],parseInt(match[1],10)-1,match[2]);
					if (tmpdate.getDate()==parseInt(match[1],10) && tmpdate.getFullYear()==parseInt(match[3],10) && (tmpdate.getMonth()+1)==parseInt(match[2],10)){
						return true;
					}
					break;
				default:
					return false;
			}
    	}
    	alert('no match on ' + strDate + '\n' + match[1]);
        return false;
    }
    else{
        return true;
    }
}

//-->
