function addLoadEvent(func) { 
  var oldonload = window.onload;
  if ( oldonload ) { 
  if (typeof window.onload != 'function') { 
    window.onload = func; 
  } else { 
    window.onload = function() { 
      if (oldonload) { 
        oldonload(); 
      } 
      func; 
    } 
  }
  } 
}  

// Manipulate Cookies
function lightswitch(){
  var light_box = document.getElementById('light_box');
  var light_child = document.getElementById('light_child');
  var light_close = document.getElementById('light_close');
  if ( light_box.style.display == 'block' ){
    light_box.style.display='none';
    light_child.style.display='none';
    light_close.style.display='none';
  }else{
    light_box.style.display='block';
    light_child.style.display='block';
    light_close.style.display='block';
  }
}


// Compact Ad Scroll
var c = 1;
function ScrollingNow(fid, fcount, ftime){
  c = c + 1;
  var ItemID = fid + c;
  //alert(ItemID);
  var ad_id = document.getElementById(ItemID);
  if ( ad_id ){
    adsetControl(fid, c, fcount);
    tabs(fid, c);
	lightin(ItemID);
  }
  if ( c == fcount){
    c = 0;
  }
}

function ScrollingAd(fid, fcount, ftime){
  var ItemID = "ad_control_"+ fid;
  var control_id = document.getElementById(ItemID);
  control_id.style.display = 'none';
  adstop = setInterval("ScrollingNow('"+ fid +"', '"+ fcount +"', '"+ ftime +"')", 1000 * ftime );
}

function ScrollingEnd(fid){
  clearInterval( adstop );
  var ItemID = "ad_control_"+ fid;
  var control_id = document.getElementById(ItemID);
  control_id.style.display = 'block';
}

function adsetControl(fid, fcontrol, fcount){
  for ( i = 1; i <= fcount; i++){
    var ItemID = "ad_control_"+ fid + i;
    var control_id = document.getElementById(ItemID);
	if ( fcontrol == i ){
	  control_id.className = "adControl_on";
    }else{
	  control_id.className = "adControl_off";
	}
  }
}

// Manipulate Lighting Effect
function lightin(fid){
  var lighinBox = document.getElementById(fid);
  lighinBox.className = "lightin_0";
  if ( lighinBox ){
    for ( i = 1; i <= 10; i++){
	  setTimeout( "lightclass('"+ fid +"', '"+ i +"')", 500 + ( i * 100 ) );
	}
  }
}
function lightclass(fid, flevel){
  var lighinBox = document.getElementById(fid);
  if ( lighinBox ){
	var itemClass = "lightin_"+ flevel;
	lighinBox.className = itemClass;
  }
}


// Manipulate Cookies
function tabs(fgroup, fid){
    for ( i = 1; i <= 20; i++){
    var boxid = fgroup + i;
    var groupbox = document.getElementById(boxid);
    if (groupbox){
	  if (i == fid){
        groupbox.style.display='block';
      }else{
        groupbox.style.display='none';
	  }
    }
  }
}


// Manipulate Cookies
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();
  }
  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);
}




function cleardefault(fid,fdefault) {
  if (document.getElementById(fid).value == fdefault){
    document.getElementById(fid).value = '';
  }
}

function ajaxload( fid, ffunction, freload ){
  // var ffirst = ffunction.replace(ffunction, /["']{1}/gi,"");
  if ( freload != 0 ){
    var fid = setInterval( ffunction, freload * 100 );
    addLoadEvent( fid );
  }
  addLoadEvent( setTimeout( ffunction, 0 ) );	  
}

function ajax( thisPage, ThisQuery, thisID, thisProgress )
{  
  if ( document.getElementById(thisID) ) {
    var WhichID = document.getElementById(thisID);
  } else {
    var WhichID = top.document.getElementById(thisID);
  }
  if ( WhichID ){
    xmlObj = "xm" + thisPage + thisID + thisProgress;
    if (window.XMLHttpRequest) {  // code for IE7+, Firefox, Chrome, Opera, Safari
      var xmlObj = new window.XMLHttpRequest();
    } else {  // code for IE6, IE5
      var xmlObj = new ActiveXObject("Microsoft.XMLHTTP"); 
    }
    xmlObj.onreadystatechange=function() 
    {
      if (document.getElementById('pageprogress')){
        document.getElementById('pageprogress').style.display='block';
	  }

	  //WhichID.innerHTML = "<img src='css/loading27.gif'>";
	  document.body.style.cursor = "wait";
	  
      if (xmlObj.readyState == 4 && xmlObj.status == 200) {
        WhichID.innerHTML = xmlObj.responseText;
		document.body.style.cursor = "default";
        if (document.getElementById('pageprogress')){
          document.getElementById('pageprogress').style.display='none';
	    }
	  //}else{
        //setTimeout( WhichID.innerHTML = xmlObj.responseText, 500 );
        //if ( StatusBox ){
          //StatusBox.style.display = 'none';
	    //}		
      }
    }
    if ( xmlObj.onreadystatechange ) {
      if ( thisProgress == 1 ) {
      }
      var fullURL = thisPage +"?psudo="+ Math.random() +"&ajaxdiv="+ thisID +"&"+ ThisQuery;
      xmlObj.open("post", fullURL, true);
      var AskStr = ThisQuery;
      xmlObj.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=utf-8");
      xmlObj.setRequestHeader("contentType", "text/html; charset=utf-8");
      xmlObj.setRequestHeader("Content-length", AskStr.length);
      xmlObj.setRequestHeader("Connection", "close");
      xmlObj.send( AskStr );
    }
  }
}


function currentYPosition(){
    if (self.pageYOffset) return self.pageYOffset;
    if (document.documentElement && document.documentElement.scrollTop)
        return document.documentElement.scrollTop;
    if (document.body.scrollTop) return document.body.scrollTop;
    return 0;
}

function elmYPosition(eID){
    var elm = document.getElementById(eID);
    var y = elm.offsetTop;
    var node = elm;
    while (node.offsetParent && node.offsetParent != document.body) {
        node = node.offsetParent;
        y += node.offsetTop;
    } return y;
}

function smoothScroll(eID) {
    var startY = currentYPosition();
    var stopY = elmYPosition(eID);
    var distance = stopY > startY ? stopY - startY : startY - stopY;
    if (distance < 100) {
        scrollTo(0, stopY); return;
    }
    var speed = Math.round(distance / 100);
    if (speed >= 20) speed = 20;
    var step = Math.round(distance / 25);
    var leapY = stopY > startY ? startY + step : startY - step;
    var timer = 0;
    if (stopY > startY) {
        for ( var i=startY; i<stopY; i+=step ) {
            setTimeout("window.scrollTo(0, "+leapY+")", timer * speed);
            leapY += step; if (leapY > stopY) leapY = stopY; timer++;
        } return;
    }
    for ( var i=startY; i>stopY; i-=step ) {
        setTimeout("window.scrollTo(0, "+leapY+")", timer * speed);
        leapY -= step; if (leapY < stopY) leapY = stopY; timer++;
    }
}

function waittoScroll(eID){
  var t = setTimeout( "smoothScroll('"+ eID +"')", 2000 );
}


// Compact Scroller
var ourInterval;
var scrollSpeed = 50;
var scrollPace = 5;
var origColor = "";
var overColor = "#FFFFCC";

function scrollStart(direction, divID, controlID){
  //document.getElementById(controlID).style.backgroundColor = overColor;
  ourInterval = setInterval("scroll"+ direction +"('"+ divID +"')", scrollSpeed);
}
function scrollEnd(which){
  clearInterval(ourInterval);
}

function scrollUp(which){
  document.getElementById(which).scrollTop = document.getElementById(which).scrollTop - scrollPace;
}
function scrollDown(which){
  document.getElementById(which).scrollTop = document.getElementById(which).scrollTop + scrollPace;
}
function scrollLeft(which){
  document.getElementById(which).scrollLeft = document.getElementById(which).scrollLeft - scrollPace;
}
function scrollRight(which){	
  document.getElementById(which).scrollLeft = document.getElementById(which).scrollLeft + scrollPace;
}


function ajaxAdmin( ThisQuery, thisID )
{  
  ajax('retrieve_admin.asp', ThisQuery, thisID, 0 );
}
function ajaxMember( ThisQuery, thisID )
{  
  ajax('retrieve_member.asp', ThisQuery, thisID, 0 );
}

function IsOk( ffunction, fstring, fdiv )
{
    var answer = confirm("You will not be able to undo the process, click ok to proceed ...");
    if (answer){
	  if (ffunction == "ajaxAdmin"){
        ajaxAdmin( fstring, fdiv ); 
	  }else{
        ajaxMember( fstring, fdiv ); 
	  }
    }else{
    }
}


// Form Tools
function toggleTabs(fid, fbutton, fnunmber, ftotal)
{
  //if(document.getElementById(fid)){
  var i = 1;
    for ( i = 1; i <= ftotal; i++){
	  if (i == fnunmber){
		if ( document.getElementById(fid + i) ){
		  document.getElementById(fid + i).style.display = 'block';
		  document.getElementById(fbutton + i).style.background = '#FFFFCC';
		}
      }else{
		if ( document.getElementById(fid + i) ){
		  document.getElementById(fid + i).style.display = 'none';
		  document.getElementById(fbutton + i).style.background = '#CCCCCC';
		}
	  }
	}
  //}
}

function textCounter(field,counter,maxlimit,linecounter) {
  var fieldWidth = parseInt(field.offsetWidth);
  var charcnt = field.value.length;        
  if (charcnt > maxlimit) { 
	field.value = field.value.substring(0, maxlimit);
  } else { 
  var percentage = parseInt(100 - (( maxlimit - charcnt) * 100)/maxlimit) ;
  document.getElementById(counter).style.width = parseInt((fieldWidth*percentage)/100)+"px";
  //document.getElementById(counter).innerHTML="Limit:"+ percentage +"%";
  document.getElementById(counter).innerHTML="";
  setcolor(document.getElementById(counter),percentage,"background-color");
  }
}
function setcolor(obj,percentage,prop){
  obj.style[prop] = "rgb(80%,"+(100-percentage)+"%,"+(100-percentage)+"%)";
}



// Form Tools
function jsRadio(fname, fidfrom, fidto) 
{
  var RadioFrom = document.forms[fname].elements[fidfrom];
  var HiddenTo = document.forms[fname].elements[fidto];
  if ( RadioFrom && HiddenTo ){
  HiddenTo.value = '';
  for (var i=0; i < RadioFrom.length; i++)
  {
    if (RadioFrom[i].checked) {
      if (HiddenTo.value != "") {
        HiddenTo.value = HiddenTo.value +", "+ RadioFrom[i].value;
      } else {
        HiddenTo.value = RadioFrom[i].value;
      }
    }
  }
  }
}

// Form Tools
function toPassword(fid) 
{
  var fPassword = document.getElementById(fid);
  if ( fPassword ){
    if(fPassword.getAttribute('type')=='text'){
      fPassword.setAttribute('type','password');
    }else{
      fPassword.setAttribute('type','text');
    }
  //fPassword.focus();
  }
}

// Form Tools
function showHide(fid) 
{
  var fdisplay = document.getElementById(fid);
  if ( fdisplay ){
    if(fdisplay.style.display == 'block'){
      fdisplay.style.display = 'none';
    }else{
      fdisplay.style.display = 'block';
    }
  }
}

// Form Tools
function setValue(fid, fvalue) 
{
  var fField = document.getElementById(fid);
  if ( fField ){
    fField.value = fvalue;
  }
}


var d = new Date()
var gmtHours = -d.getTimezoneOffset()/60;
createCookie("timezone",gmtHours,1);


function jsDefault(fid, fdefault, fvalue){
  if ( fdefault == fvalue || fvalue == '' ){
    document.getElementById(fid).value = fdefault;
  }else{
    document.getElementById(fid).value = fvalue;
  }
}

function setRating(fbar, fid, fcount, ftotal){
  var fratebar = document.getElementById(fbar);
  var frateid = document.getElementById(fid);
    
  for ( var i = 1; i <= ftotal; i += 1 ) {
	if( i <= fcount ){
      document.getElementById(fid + 1).src='default/css/rating.gif';
	}else{
      document.getElementById(fid + 1).src='default/css/norating.gif';
	}
  }
}

function jsZooming(fid,fwidth,fheight){
  document.getElementById(fid).style.width = parseInt(fwidth) +'px';
  document.getElementById(fid).style.height = parseInt(fheight) +'px';
}


function jsZoom(fsize,fid,fwidth,fheight,fzoom){
  if (fsize == 'large'){
    for ( var i = 1; i <= fzoom; i += 1 ) {
	  //setTimeout("jsZooming('"+ fid +"','"+ parseInt(fwidth) + parseInt(i) +"','"+ parseInt(fheight) + parseInt(i) +"')",3000 );
	}
    document.getElementById(fid).style.width = parseInt(fwidth) + parseInt(fzoom) +'px';
    document.getElementById(fid).style.height = parseInt(fheight) + parseInt(fzoom) +'px';
  }else{
    document.getElementById(fid).style.width = parseInt(fwidth) +'px';
    document.getElementById(fid).style.height = parseInt(fheight) +'px';
  }
}



// Upload Frame FTP
function uploadAllFrame(){
  var totalFrames = readCookie("totalframecount");
  var SendIncrement = 300;
  if ( 1 == 1 ){
    for ( i = 1; i <= totalFrames; i++){
	  var itemID = "uploadframe"+ i;
	  var itemForm = "frameform"+ i;
	  var itemImage = "uploadStatus"+ i;
	  //document.getElementById(itemID).itemForm.submit();
      window.frames[itemID].document.forms[itemForm].submit()	;
      document.getElementById(itemImage).src='http://myopensolution.com/css/icon_loading_yellow.gif';
	  setTimeout("uploadAllStatus("+ i +")",20000);
    }
  } else {
    alert('System did not detect any upload form');
  }
}

function uploadAllStatus(fid){
  var statusFrames = readCookie("statusFrame"+ fid);
  var itemID = "uploadframe"+ fid;
  var itemImage = "uploadStatus"+ fid;
  if (statusFrames == "Yes"){
    //window.frames[itemID].style.display='none';
  } else {
    window.frames[itemID].location="remote.asp?remote=frameform&error=failed&uploadnumber="+ fid;
    document.getElementById(itemImage).src='http://myopensolution.com/css/red_x.gif';
	alert("Upload Item "+ fid + " Status: failed");
  }
}



// Upload Frame FTP
function startChatBox(){
  var check_ChatBox = document.getElementById('DivChatMessage');
  var SendIncrement = 300;
  if ( check_ChatBox ){
    ajaxload( "loadingChatBox", "ajax('remote.asp', 'remote=chathistory', 'DivChatMessage', 0 )", 30 );
  }
}

// Print Item
function printHTML(fcontent){ 
var inf = fcontent;
win = window.open("print.asp", 'printme', 'toolbar=no,location=no,scrollbars=yes,status=no');
//win.document.write('<input type=button value="Print Document" style="width:100%;" onclick="window.print();">');
win.document.write('<'+'html'+'><'+'body'+'>');
win.document.write(inf);
win.document.write('<'+'/'+'body'+'><'+'/'+'html'+'>');
win.self.print();
//win.close();
}



// Upload Frame FTP
function LinkMonster(fname){
  window.open('http://www.templatemonster.com/website-templates/'+ fname +'.html','zoom','width=1000,height=800,location=no,scrollbars=yes');
}

