var appendInputTypeClasses = {
	'input' : function(element) {
        if (element.getAttribute("type")) {
            var className = element.getAttribute("type");
            addClass(element,className);
        }
		if (element.getAttribute("disabled") && element.getAttribute("disabled") == "disabled") {
			addClass(element,"disabled");
		}
    }
};
//Behaviour.register(appendInputTypeClasses);

// Dean Edwards/Matthias Miller/John Resig

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") 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;
      }
    }
  }
}



function Validate_Email(theForm)
{

  if (theForm.email.value == "")
  {
    alert("Please enter a value for the \"Email Address\" field.");
    theForm.email.focus();
    return (false);
  }

  if (theForm.email.value.length < 6)
  {
    alert("Please enter at least 6 characters in the \"Email Address\" field.");
    theForm.email.focus();
    return (false);
  }

  var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_@.";
  var checkStr = theForm.email.value;
  var allValid = true;
  var bDotPresent = false;
  var bRatePresent = false;
  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;
    }

    if (j == checkOK.length - 1  )
	    {
	    	bDotPresent = true;

	    }

	    if (j == checkOK.length -2 )
	    {
	    	bRatePresent = true;
	    }

  }

  if (!allValid)
  {
    alert("Please enter only letter, digit and \"-_@.\" characters in the \"Email Address\" field.");
    theForm.email.focus();
    return (false);
  }

  if ((!bDotPresent) || (!bRatePresent) )

    {
    	alert ("Please enter a valid email address");
    	theForm.email.focus();
    	return(false);
    }

	  return (true);
}

function openPopUp (winURL, winName, winFeatures){
	newWindow = window.open(winURL,winName,winFeatures);
	newWindow.focus();
}

function newGalleryWindow(galleryURL, galleryWinName, galleryFeatures){
	window.open(galleryURL, galleryWinName, galleryFeatures);
}

function displayWindow (url, width, height){
		var Win = window.open (url,"displayWindow",'width=' + width + ',height=' +
		height + ',resizable=0,scrollbars=no,menubar=no,status=yes');
		Win.focus();
	}

function validateIt(theForm)
 {
  if(theForm.categoryid.value != '')
	{
	theForm.submit()
	}
  else
     	{
     	return false
     	}
 }
 
function doClear(theText) {
	if (theText.value == theText.defaultValue) {
	  theText.value = ""
	}
}

function ValidateSong_AddEdit ( thisObj )
{
	if ( thisObj.strTitle.value == '' )
	{
		alert("Please enter a song title");
		thisObj.strTitle.focus();
		return false;
	}
	
	if ( thisObj.strFileURL.value == '' )
	{
		alert("Please enter a song url");
		thisObj.strFileURL.focus();
		return false;
	} 

	if ( thisObj.blnSingle.checked )
		if ( !thisObj.dblPrice[0].checked && !thisObj.dblPrice[1].checked && !thisObj.dblPrice[2].checked )
		{
			alert("Please select a price");
			return ( false );
		}

}

function ValidateAlbum_AddEdit ( thisObj )
{
	if ( thisObj.strTitle.value == '' )
	{
		alert("Please enter a album title");
		thisObj.strTitle.focus();
		return false;
	}
	

	
	if ( !thisObj.dblPrice[0].checked && !thisObj.dblPrice[1].checked && !thisObj.dblPrice[2].checked )
	{
		alert("Please select a price");
		return ( false );
	}
	
	var intCount = 0;
	for ( i = 0; i<thisObj.intDownloadGroup_Single_id.length ; i++ )
		if ( thisObj.intDownloadGroup_Single_id[i].checked ) 
			intCount++;
	
	if ( intCount < 5 || intCount > 20 ) 
	{
		alert("Albums must contain between 5 - 20 songs");
		return ( false );
	}

	
}

function submitEvent()
{
							
	var Mnth,Dth,Yer,Valid;
	Valid="TRUE";
	Mnth = document.EventProcessing.intMonth.value;
	Dth = document.EventProcessing.intDay.value;
	Yer = document.EventProcessing.intYear.value;
	if(Mnth == 2 || Mnth == 4 || Mnth == 6 || Mnth == 9 || Mnth == 11)
	{
		if(Mnth == 2 && (Yer%4)!=0 && Dth>28)
			{
				alert("Please select a valid Date/Month");
				Valid="FALSE";
			}
		else
			{
			if(Dth>30)
				{
				alert("Please select a valid Date/Month");
				Valid="FALSE";
				}
			}	
	}
	if(Valid == "TRUE")
	{
		document.EventProcessing.action="Reservation_Create_Proc.asp";
		document.EventProcessing.submit();
	}
}

function submitDeleteEvent(id)
{			
	var blnAsk;
	blnAsk = confirm("You are about to delete an Event. Would you like to proceed?");
	if (blnAsk)
	{
		document.location.href="Reservation_Delete_Proc.asp?intReservationItem_id=" +  id ;
	}
}

function validateIt(theForm)
 {
  if(theForm.categoryid.value != '')
	{
	theForm.submit()
	}
  else
     	{
     	return false
     	}
 }
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=300');
	}

function Email_Validator(theForm)
{


  if (theForm.email.value == "")
  {
    alert("Please enter a value for the \"Email Address\" field.");
    theForm.email.focus();
    return (false);
  }

  if (theForm.email.value.length < 6)
  {
    alert("Please enter at least 6 characters in the \"Email Address\" field.");
    theForm.email.focus();
    return (false);
  }

  var checkOK = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_@.";
  var checkStr = theForm.email.value;
  var allValid = true;
  var bDotPresent = false;
  var bRatePresent = false;
  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;
    }

    if (j == checkOK.length - 1  )
	    {
	    	bDotPresent = true;

	    }

	    if (j == checkOK.length -2 )
	    {
	    	bRatePresent = true;
	    }

  }

  if (!allValid)
  {
    alert("Please enter only letter, digit and \"-_@.\" characters in the \"Email Address\" field.");
    theForm.email.focus();
    return (false);
  }

  if ((!bDotPresent) || (!bRatePresent) )

    {
    	alert ("Please enter a valid email address");
    	theForm.email.focus();
    	return(false);
    }

	  return (true);
}

function ValidateEmail(str)
 {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 ||str.indexOf(at)==lstr){
		   alert("Invalid E-mail ID")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 ||
str.indexOf(dot)==lstr){
		    alert("Invalid E-mail ID")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot ||
str.substring(lat+1,lat+2)==dot){
		    alert("Invalid E-mail ID")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("Invalid E-mail ID")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    alert("Invalid E-mail ID")
		    return false
		 }

 		 return true					
	}
	
	function RateReview_RateReview_Validate(objForm)
{
	var blnOutReturnVal;
	var blnInReturnVal;	
	blnOutReturnVal = RateReview_TextCheck(objForm.strRateDesc, "Comments");
	if (blnOutReturnVal)
	{		
		
		blnInReturnVal = RadioCheck(objForm, "rating");
			if (blnInReturnVal)	
			{
				return true;
			}
			else
			{
				return false;		
			}
	}
	else
	{
		return false;
	}
}
	

function RateReview_TextCheck(objTextBox, objName)
{
	if (objTextBox.value == "")
	{
		alert("Please Ensure that you have Entered data under " + objName )
		return false;
	}  
	else
	{
		return true;
	}
}
	
	
function RateReview_RadioCheck(objForm, objName)
{	
	
	var i=0;
	var obj;
	var intCheckCount=0;
	
	for (i=0;i<objForm.elements.length;i++)
	{
		obj = objForm.elements[i];
		if ((obj.type == "radio") && (obj.checked))
		{
			intCheckCount++;
		}		
	}
	
	if (intCheckCount>0)
	{
		return true;
	}
	else
	{
		alert("Please ensure you have selected " + objName + ".");
		return false;
	}	
}

/*function trimString(str)
{ 
	return str.replace(/^\s+/,'').replace(/\s+$/,'');
}*/

function validateForm(whichform) {

  var validate = new Object();
    for (var i=0; i<whichform.elements.length; i++)
    {
        var element = whichform.elements[i];
        //added the function to alert the user if he selects country as United states and state as blank.
        //Savio Fernandes - September 9, 2006
        
        if(element.className.indexOf("checkstate") != -1)
        {
            if (document.EditShipping.strShippingAddress_country.value =="United States" && element.value=="")
            {
              
	                var elTitle = element.title || element.name;
	                alert("Please fill in the "+elTitle+" field.");
	                element.focus();
                    return false;
            }
         }
        
        
         
        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("username") != -1)
        {
            if (!isFullUsername(element))
            {
                var elTitle = element.title || element.name;
                alert("The "+elTitle+" field must be at least 6 - 30 characters in length.");
                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 (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 isFullUsername(field)
 {
    var strtemp = trimString(field.value);
    if (strtemp.length < 6 || strtemp.length > 30)
    {
        return false;
    }
    else
    {
        return true;
    }
}

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 isValidPassword(field) {
	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 prepareForms() {
  for (var i=0; i<document.forms.length; i++) {
    var thisform = document.forms[i];    
    if ( thisform.name == "frmPayment" ||  thisform.name == "frmCheckOut") continue;
    resetFields(thisform);
	/*if (thisform.addEventListener) {
		thisform.addEventListener('submit', function(){return validateForm(this);}, false);
	} else if (thisform.attachEvent) {
		thisform.attachEvent('onsubmit', function(){return validateForm(this);});
	}*/
     if (thisform.name !="EditShipping") {
     	
    thisform.onsubmit = function() {
      return validateForm(this);      
    }
    }
  }
}