var crip = null;
function NewPopupWindow(url, awdth)
{
	if((crip == null) || (crip.closed)) 
	{
		crip = window.open(url, 'popupwindow', 'toolbar=no,directories=no,status=no,location=no,menubar=no,scrollbars=yes,width='+awdth+',height=480,left=10,top=10,resizable=yes');
		crip.focus();
	}
	else
	{
		crip.location = url;
		crip.focus();
	}
}

var crip = null;
function NewPopupWindowWithNav(url, awdth)
{
	if((crip == null) || (crip.closed)) 
	{
		crip = window.open(url, 'popupwindow2', 'toolbar=yes,menubar=yes,scrollbars=yes,width='+awdth+',height=480,left=10,top=10,resizable=yes');
		crip.focus();
	}
	else
	{
		crip.location = url;
		crip.focus();
	}
}

function doPreload()
	{
		var the_images = new Array('images/1700s-on.jpg','images/1800s-on.jpg','images/1935s-on.jpg','images/about-on.jpg','images/help-on.jpg','images/contact-on.jpg');
		preloadImages(the_images);
	}

function preloadImages(the_images_array) 
	{
		for(loop = 0; loop < the_images_array.length; loop++)
			{
		   		var an_image = new Image();
				an_image.src = the_images_array[loop];
			}
	}

function CheckForm()
	{
		if (document.aLogin.aUsername.value == "")
			{
				alert("Please enter your Username");
				return false;
			}
		else if (document.aLogin.aPassword.value == "")
			{
				alert("Please enter your Password");
				return false;
			}
		else
			{
				return true;
			}
	}
	
function CheckAdminForm()
	{
		if (document.aAdminInfo.aFirstname.value == "")
			{
				alert("Please enter the First Name");
				return false;
			}
		else if (document.aAdminInfo.aLastname.value == "")
			{
				alert("Please enter the Last Name");
				return false;
			}
		else if (document.aAdminInfo.aUsername.value == "")
			{
				alert("Please enter the Username");
				return false;
			}
		else if (document.aAdminInfo.aPassword.value == "")
			{
				alert("Please enter the Password");
				return false;
			}
		else if (document.aAdminInfo.aEmail.value == "")
			{
				alert("Please enter the Email Address");
				return false;
			}
		else
			{
				return true;
			}
	}

function amoveBetweenSelects(from,ato2)
{
   for(i=0; i < from.length; i++)
   {
      if(from.options[i].selected)
        {
         j=0;
         alreadyInList = false;
         while((j < ato2.length) && (ato2.options[j].text))
		  {
            if(ato2.options[j].text == from.options[i].text)
            {
               alreadyInList=true;
            }
            j++;
           }
         if(j < ato2.length && !alreadyInList)
         {
            ato2.options[j].text =  from.options[i].text;
            ato2.options[j].value = from.options[i].value;
			var membername = ato2.options[j].value;
			var aviewmembername = from.options[i].text;
            document.users.aGroupIDs.value = document.users.aGroupIDs.value + membername + ",";			
         }
       }
	   
    }
}

function aremoveFromSelect(slect)
{
   document.users.aGroupIDs.value = "";
   for(i=1; i < slect.length; i++)
   {
      if(slect.options[0].selected)
	  {
	   alert("You cannot remove this item. This item will not be added to the list. So please ignore it.")
	   break;
	  }
      if(slect.options[i].selected)
      {
         slect.options[i] = null;				 
        i--;
      }	 
	  else
	  {
	    var membername = slect.options[i].value;
		var aviewmembername = slect.options[i].text;
		if (membername != "")
		 {
          document.users.aGroupIDs.value = document.users.aGroupIDs.value + membername + ",";
		 }		
	  }  
   }    
}

function CheckCustomerForm()
{
	var aIP = document.users.aIPAddress.value;
	var index = aIP.indexOf(".");
	var i = 0;
	while (index != -1) {
		i = i + 1
		index = aIP.indexOf(".", index+1);		
	}
	if (i != 3)
	{
		alert("Please enter the correct IPAddress. Eg: 100.2.3.4 or 209.334.*.*");
		return false
	}	
	else if (document.users.ato2.options[1].value == "")
	{
		alert("Please assign the Customer to at least one Group");
		return false;
	}
	else if (isFloat(document.users.aPrice.value) == "false")
	{
		alert("The Price per Article has to be an integer. Ex: 9, 10.5");
		return false;
	}
	else
	{
		return true;
	}
	
}

function CheckCustomerPOForm()
{
	if (document.ponumbers.aPONumber.value == "")
	{
		alert("Please enter the PONumber");
		return false;
	}
	else if (document.ponumbers.aMaxAmount.value == "")
	{
		alert("Please enter the Maximum Amount");
		return false;
	}
	else 
	{
		return true;
	}
}

function isFloat(sNum)
{
		var new_msg = "true"
		inputStr = sNum.toString()
		for (var i = 0; i < inputStr.length; i++)
			{
			var oneChar = inputStr.charAt(i)			
			if ((oneChar < "0" || oneChar > "9") && oneChar != ".")
					{
						new_msg = "false"
					}
			}
		return (new_msg)
}

function isInteger(sNum)
{
		var new_msg = "true"
		inputStr = sNum.toString()
		for (var i = 0; i < inputStr.length; i++)
			{
			var oneChar = inputStr.charAt(i)			
			if ((oneChar < "0" || oneChar > "9"))
					{
						new_msg = "false"
					}
			}
		return (new_msg)
}

function CheckQTYForm()
{
	if (document.aEditQty.aQuantity.value == "")
	{
		alert("Please enter the Quantity");
		return false;
	}
	else if (isInteger(document.aEditQty.aQuantity.value) == "false")
	{
		alert("The Quantity must be an integer.");
		return false;
	}
	else 
	{
		return true;
	}
}

function CheckSubmitOrderForm()
{
	if (document.aSubmitOrder.PONumber.value == "")
	{
		alert("Please enter the PO Number field");
		return false;
	}
	if (document.aSubmitOrder.RequestedBy.value == "")
	{
		alert("Please enter the Requested By field");
		return false;
	}
	else 
	{
		return true;
	}
}

function CheckEmailForm()
{
	if (document.aSendEmail.aTo.value == "")
	{
		alert("Please enter the E-mail address.");
		return false;
	}
	else if (document.aSendEmail.aSubject.value == "")
	{
		alert("Please enter the Subject.");
		return false;
	}
	else
	{
		return true;
	}
}

function isDate(dateStr) {

    var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/;
    var matchArray = dateStr.match(datePat); // is the format ok?

    if (matchArray == null) {
        alert("Please enter date as mm/dd/yyyy.");
        return false;
    }

    month = matchArray[1]; // parse date into variables
    day = matchArray[3];
    year = matchArray[5];

    if (month < 1 || month > 12) { // check month range
        alert("Month must be between 1 and 12.");
        return false;
    }

    if (day < 1 || day > 31) {
        alert("Day must be between 1 and 31.");
        return false;
    }

    if ((month==4 || month==6 || month==9 || month==11) && day==31) {
        alert("Month "+month+" doesn't have 31 days!")
        return false;
    }

    if (month == 2) { // check for february 29th
        var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
        if (day > 29 || (day==29 && !isleap)) {
            alert("February " + year + " doesn't have " + day + " days!");
            return false;
        }
    }
    return true; // date is valid
}

function CheckHitForm()
{
	if (document.aSiteHits.aStartDate.value == "")
	{
		alert("Please enter the Start Date.");
		return false;
	}
	else if (!isDate(document.aSiteHits.aStartDate.value))
	{
		return false;
	}
	else if (document.aSiteHits.aEndDate.value == "")
	{
		alert("Please enter the End Date.");
		return false;
	}
	else if (!isDate(document.aSiteHits.aEndDate.value))
	{
		return false;
	}
	else
	{
		return true;
	}
}
