
function init() {
    // quit if this function has already been called
    if (arguments.callee.done) return;
    // flag this function so we don't do the same thing twice
    arguments.callee.done = true;
    // kill the timer
    if (_timer) clearInterval(_timer);
	
    // do stuff	    
	prepareForms();
};

/* for Mozilla/Opera9 */
if (document.addEventListener) {
    document.addEventListener("DOMContentLoaded", init, false);
}

/* for Internet Explorer */
/*@cc_on @*/
/*@if (@_win32)
    document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");
    var script = document.getElementById("__ie_onload");
    script.onreadystatechange = function() {
        if (this.readyState == "complete") {
            init(); // call the onload handler
        }
    };
/*@end @*/

/* for Safari */
if (/KHTML|WebKit/i.test(navigator.userAgent)) { // sniff
    var _timer = setInterval(function() {
        if (/loaded|complete/.test(document.readyState)) {
            init(); // call the onload handler
        }
    }, 10);
}

/* for other browsers */
window.onload = init;

String.prototype.trim = function() {
	// Strip leading and trailing white-space
	return this.replace(/^\s*|\s*$/g, "");
}

String.prototype.normalize_space = function() {
	// Replace repeated spaces, newlines and tabs with a single space
	return this.replace(/^\s*|\s(?=\s)|\s*$/g, "");
}


function insertAfter(newElement,targetElement) {
  var parent = targetElement.parentNode;
  if (parent.lastChild == targetElement) {
    parent.appendChild(newElement);
  } else {
    parent.insertBefore(newElement,targetElement.nextSibling);
  }
}

function addClass(element, className) {
	if (!hasClass(element, className)) {
		if (element.className) element.className += " " + className;
		else element.className = className;
	}
}

function removeClass(element, className) {
	var regexp = new RegExp("(^|\\s)" + className + "(\\s|$)");
	element.className = element.className.replace(regexp, "$2");
};

function hasClass(element, className) {
	var regexp = new RegExp("(^|\\s)" + className + "(\\s|$)");
	return regexp.test(element.className);
};

function focusLabels() {
	if (!document.getElementsByTagName) return false;
	var labels = document.getElementsByTagName("label");
		for (var i=0; i<labels.length; i++)
		 {
		    labels[i].onclick = function() {
			    if(this.getAttribute("for"))
			     {
				       var id = this.getAttribute("for");
				       if (!document.getElementById(id)) return false;
				       var element = document.getElementById(id);
			     }
			     else
			     {
				       if (!this.getElementsByTagName("input")) return false;
				       var element = this.getElementsByTagName("input")[0];				
			     }
			    element.focus();
		 }
	}
}

function resetFields(whichform) {
  var inputs = whichform.getElementsByTagName("input");  
  for (var i=0, len=inputs.length; i<len; i++) {
	var input = inputs[i];
    if (input.type == "submit" || input.type == "button" || input.type == "reset" || input.type == "image" || input.type == "checkbox" || input.type == "hidden") continue;
    //if (!input.defaultValue) continue;
    //if (!input.defaultValue) input.defaultValue = input.value;
    input.onfocus = function() {
    if (this.value == this.defaultValue) {
      //this.value = "";
      this.select(this.value.length);
     }
    }
    input.onblur = function() {
      if (this.value == "") {
        this.value = this.defaultValue;
      }
    }
  }
}
// Modified by Savio Fernandes on Aug 17, 2006.
function validateForm(whichform)
 {
    
    var validate = new Object();
    for (var i=0; i<whichform.elements.length; i++)
    {
        var element = whichform.elements[i];
        if (element.className.indexOf("required") != -1)
        {
            if (!isFilled(element))
            {
	            var elTitle = element.title || element.name;
	            alert("Please fill in the "+elTitle+" field.");
	            element.value = trimString(element.value);
	            element.focus();
                return false;
            }
            element.value = trimString(element.value);
        }
    
        if (element.className.indexOf("changedefault") != -1)
        {
    	    if (element.value == element.defaultValue )
    	    {
    		    var elTitle = element.title || element.name;
        	    alert("Please fill in the "+elTitle+" field.");
        	    element.value = "";
        	    element.focus();
        	    return false;	
            }    
        }
        
        if (element.className.indexOf("email") != -1)
        {
            if (!isEmail(element))
            {
		        var elTitle = element.title || element.name;
                alert("The "+elTitle+" field must be a valid email address.");
                element.focus();
                return false;
            }
        }
        
        if (element.className.indexOf("password") != -1)
         {
            if (!isFullPassword(element))
             {
		        var elTitle = element.title || element.name;
                alert("The "+elTitle+" field must be at least 6 - 20 characters in length.");
                element.focus();
                return false;
             }/*
             else
              {*/
		    if (!hasClass(element,"confirmpass")) validate.password = element.value;
	          /*}*/
            /* Doesn't work, didn't really spend much time on it*/
	        if (!isValidPassword(element))
	         {
	                var elTitle = element.title || element.name;
                    alert("The "+elTitle+" field contains invalid characters.");
                    element.focus();
                    return false;
            }
        }
        
        if (element.className.indexOf("confirmpass") != -1)
         {
            if (!validate.password || validate.password && (element.value != validate.password))
            {
                alert("The passwords you entered don't match.");
                element.focus();
                return false;
            }
         }
    }
  return true;
}

function isFilled(field) {
 var strtemp = trimString(field.value);
  if (field.type == "checkbox") {
  	if (!field.checked)
  		return false;
	else
		return true;
	
  }
  else {
	  if (strtemp.length < 1) { // || field.value == field.defaultValue
	    return false;
	  } else {
	    return true;
	  }
  }	  
}

function isEmail(field)
 {
  var regexpEmail = /\w{1,}[@][\w\-]{1,}([.]([\w\-]{1,})){1,3}$/;
  return regexpEmail.test(field.value);
}

function isFullPassword(field) {
  if (field.value.length < 6 || field.value.length > 20) {
    return false;
  } else {
    return true;
  }
}
function isValidPassword(field) {
	//var regexp = new RegExp(!"/[-A-Za-z1-9]/");
	//return regexp.test(field.value);
	var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-";
	var checkStr = field.value;
	var allValid = true;

	for (i = 0;  i < checkStr.length;  i++)
	{
	  ch = checkStr.charAt(i);
	  for (j = 0;  j < checkOK.length;  j++)
	    if (ch == checkOK.charAt(j))
	      break;
	  if (j == checkOK.length)
	  {
	    allValid = false;
	    break;
	  }
	}
	return allValid;
}

function trimString(sString)
 {
	while (sString.substring(0,1) == ' ')
	{
		sString = sString.substring(1, sString.length);
	}
	
	while (sString.substring(sString.length-1, sString.length) == ' ')
	 {
		sString = sString.substring(0,sString.length-1);
	}
	
	return sString;
}


function prepareForms() {
  for (var i=0; i<document.forms.length; i++) {
    var thisform = document.forms[i];    
    if ( thisform.name == "frmPayment" ||  thisform.name == "frmCheckOut" || thisform.name =="EditShipping" || thisform.name =="submitSearch") continue;
    resetFields(thisform);      	
    thisform.onsubmit = function() {
      return validateForm(this);          
    }
  }
}

function password(){
	self.name = "Parent_Window"; 
	window.open('/portal/user/send_pass.asp','EmailLoginInfo','toolbar=no,location=no,directories=no,status=no,scrollbars=no,resizable=no,copyhistory=no,width=350,height=260');
}
function MyAccount_DeleteSubaccont(intAssociation_id, deassociate)
{
    var ask;
    if ( deassociate == 'NO' )
    	ask = confirm("You are about to enable a Sub Account. Do you want to continue?");
    else
    	ask = confirm("You are about to disable a Sub Account. Do you want to continue?");
    	
    if(ask)
    {
        //alert("/portal/user/deletesubaccount_proc.asp?intUserAssociation_id=" + intAssociation_id);
        //return;
        document.location.href = "/portal/user/deletesubaccount_proc.asp?intUserAssociation_id=" + intAssociation_id + "&deassociate=" + deassociate;
    }
}
