var crip = null;
function NewPopupWindow(url)
{
	if((crip == null) || (crip.closed)) 
	{
		crip = window.open(url, 'popupwindow', 'toolbar=no,directories=no,status=no,location=no,menubar=no,scrollbars=yes,width=650,height=600,left=20,top=20,resizable=yes');
	}
	else
	{
		crip.location = url;
	}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_findObj(n, d) { //v3.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
function CheckVendorForm()
{
	var aSubmit = "no";
	if (document.aVendorsFormSubmit.aZipCode.value == "")
		{
			if (document.aVendorsFormSubmit.aCity.value == "" && document.aVendorsFormSubmit.aState.value == "")
				{
					alert("Please enter the City & State or Please enter a Zip Code");
					return false;
				}
			else if (document.aVendorsFormSubmit.aCity.value == "")
				{
					alert("Please enter the City");
					return false;				
				}
			else if (document.aVendorsFormSubmit.aState.value == "")
				{
					alert("Please select a State");
					return false;
				}
			else
				{
					aSubmit = "yes";
				}
		}
	else
		{
			aSubmit = "yes";
		}	
	
	if (aSubmit == "yes")
	{
		if (document.aVendorsFormSubmit.aSubCategoryID.value == "-1")
		{
			alert("You cannot select a Category Name. Please select a SubCategory.");		
			return false;
		}
		else
		{
			return true;
		}
	}
}

function CheckJobForm()
{
	var aSubmit = "no";
	if (document.aJobsFormSubmit.aZipCode.value == "")
		{
			if (document.aJobsFormSubmit.aCity.value == "" && document.aJobsFormSubmit.aState.value == "")
				{
					alert("Please enter the City & State or Please enter a Zip Code");
					return false;
				}
			else if (document.aJobsFormSubmit.aCity.value == "")
				{
					alert("Please enter the City");
					return false;				
				}
			else if (document.aJobsFormSubmit.aState.value == "")
				{
					alert("Please select a State");
					return false;
				}
			else
				{
					aSubmit = "yes";
				}
		}
	else
		{
			aSubmit = "yes";
		}	
		
	if (aSubmit == "yes")
	{
		if (document.aJobsFormSubmit.aSubCategoryID.value == "-1")
		{
			alert("You cannot select a Category Name. Please select a SubCategory.");		
			return false;
		}
		else
		{
			return true;
		}
	}
}

function aSearch(aValue)
{
	document.aExtendedSearch.aSearchRange.value = aValue;
	document.aExtendedSearch.submit();
	
}

function GetLatLong()
{
	//if (document.aClientForm.aAddress.value == "")
	//{
		//alert("Please enter the Address");
	//}
	//else 
	if (document.aClientForm.aCity.value == "")
	{
		alert("Please enter the City");
	}
	else if (document.aClientForm.aState.value == "")
	{
		alert("Please enter the State");
	}
	else if (document.aClientForm.aZip.value == "")
	{
		alert("Please enter the Zip");
	}
	else
	{
		TheAddress = document.aClientForm.aAddress.value;
		TheCity = document.aClientForm.aCity.value;
		TheState = document.aClientForm.aState.value;
		TheZip = document.aClientForm.aZip.value;
		aURL = "../../GetLatLong/GeoCode.cfm?address="+ escape(TheAddress) +"&city="+ TheCity + "&state="+ TheState +"&zipcode="+ TheZip +"&aFrom=C" ;
		NewPopupWindow(aURL);
	}
}

function EnDisBDate()
{
	if (document.aClientForm.aStatusID.value == "4")
	{
		document.aClientForm.aBillingDate.disabled = true;
	}
	else
	{
		document.aClientForm.aBillingDate.disabled = false;
	}
}

function AddFields()
{
	aBQ = parseFloat(document.aInForm.aBannerQty.value);
	aLQ = parseFloat(document.aInForm.aLogoQty.value);
	aLiQ = parseFloat(document.aInForm.aListingQty.value);
	aJQ = parseFloat(document.aInForm.aJobPostingQty.value);
	aFQ = parseFloat(document.aInForm.aFeaturedVendorQty.value);
	aB = parseFloat(document.aInForm.aBannerCharge.value);
	aL = parseFloat(document.aInForm.aLogoCharge.value);
	aLi = parseFloat(document.aInForm.aListingCharge.value);
	aF = parseFloat(document.aInForm.aFeaturedVendorCharge.value);
	aJ = parseFloat(document.aInForm.aJobPostingCharge.value);
	var aCatVal = 0;
	var vLoopNum = document.aInForm.numberOfCats.value;
	for (var c = 1; c <= vLoopNum; c++) {
		var formVal = "document.aInForm.aCatCharges_"+ c +".value";
		aCatVal = eval(aCatVal + parseFloat(eval(formVal)));
	}
	aTot = (aBQ * aB) + (aLQ * aL) + (aLiQ * aLi) + (aJQ * aJ) + (aFQ * aF) + aCatVal;
	var n = aTot;	
	n = "" + ((Math.round(n * 100)) / 100);
	a = n.substring(n.length-3, n.length-2);
	b = n.substring(n.length-2, n.length-1);
	if (a != '.') {
	if (b == '.') n += "0";
	else n += ".00";}	
	document.aInForm.aTotalAmt.value = n;
}

function GetJobLatLong()
{
	//if (document.aSubForm.aContactAddress.value == "")
	//{
	//alert("Please enter the Address");
	//}
	//else 
	if (document.aSubForm.aContactCity.value == "")
	{
		alert("Please enter the City");
	}
	else if (document.aSubForm.aContactState.value == "")
	{
		alert("Please enter the State");
	}
	//else if (document.aSubForm.aContactZip.value == "")
	//{
	//	alert("Please enter the Zip");
	//}
	else
	{
		TheAddress = document.aSubForm.aContactAddress.value;
		TheCity = document.aSubForm.aContactCity.value;
		TheState = document.aSubForm.aContactState.value;
		TheZip = document.aSubForm.aContactZip.value;
		aURL = "../../GetLatLong/GeoCode.cfm?address="+ TheAddress +"&city="+ TheCity + "&state="+ TheState +"&zipcode="+ TheZip +"&aFrom=C&aJ=1" ;
		NewPopupWindow(aURL);
	}
}

function moveBetweenSelects(from,ato)
{
    if (from.length != ato.length)
	{
		 for(k=ato.length; k<from.length; k++)
		{
			var optName = new Option("", "");
			ato.options[k] = optName;
		}
	}
   for(i=0; i < from.length; i++)
   {
      if(from.options[i].selected)
        {
         j=0;
         alreadyInList = false;
         while((j < ato.length) && (ato.options[j].text))
		  {
            //if(ato.options[j].text == from.options[i].text)
				if(ato.options[j].value == from.options[i].value && ato.options[j].text == from.options[i].text)
            {
               alreadyInList=true;
            }
            j++;
           }
         if(j < ato.length && !alreadyInList)
         {
				ato.options[j].text =  from.options[i].text;
            ato.options[j].value = from.options[i].value;
				var aSubCategoryID = ato.options[j].value;
				document.aSubForm.SubCategoryIDs.value = document.aSubForm.SubCategoryIDs.value + aSubCategoryID + ",";				
         }
       }	   
    }
}

function removeFromSelect(slect)
{
   document.aSubForm.SubCategoryIDs.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 Categories list. So please ignore it.")
	   break;
	  }
      if(slect.options[i].selected)
      {	  	
         slect.options[i] = null;	
         i--;
      }	 
	  else
	  {	    
		var aSubCategoryID = slect.options[i].value;
		if (aSubCategoryID != "")
		 {
		  document.aSubForm.SubCategoryIDs.value = document.aSubForm.SubCategoryIDs.value + aSubCategoryID + ",";
		 }	
	  }  
   }
}


function moveBetweenSelects_2(from,ato)
{
    if (from.length != ato.length)
	{
	  for(k=ato.length; k<from.length; k++)
		{
			var optName = new Option("", "");
			ato.options[k] = optName;
		}
	}
   for(i=0; i < from.length; i++)
   {
      if(from.options[i].selected)
        {
         j=0;
         alreadyInList = false;
         while((j < ato.length) && (ato.options[j].text))
		  {
            if(ato.options[j].text == from.options[i].text)
            {
               alreadyInList=true;
            }
            j++;
           }
         if(j < ato.length && !alreadyInList)
         {
			ato.options[j].text =  from.options[i].text;
            ato.options[j].value = from.options[i].value;
			var aSubscriptionTypeID = ato.options[j].value;
			document.aSubForm.SubscriptionTypeIDs.value = document.aSubForm.SubscriptionTypeIDs.value + aSubscriptionTypeID + ",";				
         }
       }	   
    }
}

function removeFromSelect_2(slect)
{
   document.aSubForm.SubscriptionTypeIDs.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 Categories list. So please ignore it.")
	   break;
	  }
      if(slect.options[i].selected)
      {	  	
         slect.options[i] = null;	
         i--;
      }	 
	  else
	  {	    
		var aSubscriptionTypeID = slect.options[i].value;
		if (aSubscriptionTypeID != "")
		 {
		  document.aSubForm.SubscriptionTypeIDs.value = document.aSubForm.SubscriptionTypeIDs.value + aSubscriptionTypeID + ",";
		 }	
	  }  
   }
}

function CheckSubForm()
{
	AbortCode = false;
	aMessg = "";
	if (document.aSubForm.SubscriptionTypeIDs.value == "")
	{
		alert("Please select at least one Posting");
		AbortCode = true;
	}
	else if (document.aSubForm.SubCategoryIDs.value == "")
	{
		alert("Please select at least one SubCategory");
		AbortCode = true;
	}
	
	if (AbortCode == false)
	{
		for (i=1; i<document.aSubForm.sSubTypes.length; i++)
		{
			if(document.aSubForm.sSubTypes[i].value != "")
			{
				aSubID = document.aSubForm.sSubTypes[i].value;
				if (aSubID == 1 && (document.aSubForm.aListingPrice.value == ""))
				{
					if (aMessg == "")
					{
						aMessg = "Please enter the Listing Price \n If you do not wish to enter a price. Please enter 0";
					}
					else
					{
						aMessg = aMessg + ", Listing Price";
					}
				}
				else if (aSubID == 2 && (document.aSubForm.aBannerPrice.value == ""))
				{
					if (aMessg == "")
					{
						aMessg = "Please enter the Banner Price \n If you do not wish to enter a price. Please enter 0";
					}
					else
					{
						aMessg = aMessg + ", Banner Price";
					}
				}
				else if (aSubID == 3 && (document.aSubForm.aLogoPrice.value == ""))
				{
					if (aMessg == "")
					{
						aMessg = "Please enter the Logo Price \n If you do not wish to enter a price. Please enter 0";
					}
					else
					{
						aMessg = aMessg + ", Logo Price";
					}
				}				
			}
		}
	}
	
	if (aMessg != "")
	{
		alert(aMessg);
		AbortCode = true;
	}
		
	if (AbortCode == true)
	{
		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 CheckCallHistoryReportForm()
{
	if (document.aCallSearch.aStartDate.value == "")
	{
		alert("Please enter the Start Date");
		return false;
	}
	else if (!isDate(document.aCallSearch.aStartDate.value))
	{
		return false;
	}
	else if (document.aCallSearch.aEndDate.value == "")
	{
		alert("Please enter the End Date");
		return false;
	}
	else if (!isDate(document.aCallSearch.aEndDate.value))
	{
		return false;
	}
	else if (document.aCallSearch.aSearchType.value != "" && document.aCallSearch.aSearchValue.value == "")
	{
		alert("Please enter the Search Value");
		return false;
	}
	else
	{
		return true;
	}
}

function CheckInvForm()
{
	if (document.aInForm.aInvoiceDate.value == "")
	{
		alert("Please enter the Invoice Date");
		return false;
	}
	else if (document.aInForm.aTotalAmt.value == "NaN.00")
	{
		alert("Please enter the right Qty and Price.");
		return false;
	}
	else
	{
		return true;
	}
}

function FormatPhone(aName, aField)
{
	var aAreaCode = "";
	var aPhonePart = "";
	var bPhonePart = "";	
	var aLen = aName.length;
	if (aLen == 7)
	{
		aPhonePart = aName.substring(0, 3);
		bPhonePart = aName.substring(3, 7);
		aField.value = aPhonePart + "-" + bPhonePart;
	}
	else if (aLen == 10)
	{
		aAreaCode = aName.substring(0, 3);
		aPhonePart = aName.substring(3, 6);
		bPhonePart = aName.substring(6, 10);
		aField.value = aAreaCode + "-" + aPhonePart + "-" + bPhonePart;
	}
}

function ConfirmDelete(aURL, aName)
{
	aConfirm = confirm("Are you sure you want to delete this " + aName + "?");
	if (aConfirm == true)
	{
		document.location = aURL;
	}	
}

function ConfirmDup(aURL, aName)
{
	aConfirm = confirm("Are you sure you want to duplicate this " + aName + "?");
	if (aConfirm == true)
	{
		document.location = aURL;
	}	
}

function CheckFaxEmail()
{
		if (document.all["DivArea"].style.visibility == "hidden")
		{
 			ToggleHTMLPreview('Preview');
		}
		if (document.all["DivArea"].innerHTML == "")
		{
			document.all["DivArea"].innerHTML = document.all["aDescription"].value;
		}		
		document.aClientForm.aDescription.value = document.all["DivArea"].innerHTML;
		return true;
}

function DisableFields()
		{
			aValue = "";
			for(i=0;i<document.aRec.aPaymentType.length;i++)
			{
				if(document.aRec.aPaymentType[i].checked)
				{
					aValue = document.aRec.aPaymentType[i].value;
				}
			}
			if (aValue == "Check")
			{
				document.aRec.sjname.disabled = true;
				document.aRec.aCCType.disabled = true;
				document.aRec.Accountnumber.disabled = true;
				document.aRec.Month.disabled = true;
				document.aRec.Year.disabled = true;
				document.aRec.StreetAddress.disabled = true;
				document.aRec.City.disabled = true;
				document.aRec.State.disabled = true;
				document.aRec.Zipcode.disabled = true;
				document.aRec.Email.disabled = true;
				document.aRec.Shiptophone.disabled = true;
				document.aRec.aCheckNum.disabled = false;
			}
			else
			{
				document.aRec.sjname.disabled = false;
				document.aRec.aCCType.disabled = false;
				document.aRec.Accountnumber.disabled = false;
				document.aRec.Month.disabled = false;
				document.aRec.Year.disabled = false;
				document.aRec.StreetAddress.disabled = false;
				document.aRec.City.disabled = false;
				document.aRec.State.disabled = false;
				document.aRec.Zipcode.disabled = false;
				document.aRec.Email.disabled = false;
				document.aRec.Shiptophone.disabled = false;
				document.aRec.aCheckNum.disabled = true;
			}
		}
		
function CheckReceiptForm()
		{
			aValue = "";
			/*for(i=0;i<document.aRec.aPaymentType.length;i++)
			{
				if(document.aRec.aPaymentType[i].checked)
				{
					aValue = document.aRec.aPaymentType[i].value;
				}
			}
			if (aValue == "Check")
			{*/
				if (document.aRec.aReceiptDate.value == "")
				{
					alert("Please enter the Receipt Date");
					return false;					
				}
				else if (document.aRec.TransactionAmount.value == "")
				{
					alert("Please enter the Amount Received");
					return false;
				}
				else if (document.aRec.aCheckNum.value == "")
				{
					alert("Please enter the check number.");
					return false;
				}
				
			/*}
			else
			{
				if (document.aRec.aReceiptDate.value == "")
				{
					alert("Please enter the Receipt Date");
					return false;					
				}
				else if (document.aRec.TransactionAmount.value == "" || document.aRec.TransactionAmount.value == "0.00" || document.aRec.TransactionAmount.value == "0")
				{
					alert("Please enter the Amount Received");
					return false;
				}
				else if (document.aRec.sjname.value == "")
				{
					alert("Please enter the Name on Card");
					return false;
				}
				else if (document.aRec.aCCType.value == "")
				{
					alert("Please select the Card Type");
					return false;
				}
				else if (document.aRec.Accountnumber.value == "")
				{
					alert("Please enter the Credit Card Number");
					return false;
				}
				else if (document.aRec.StreetAddress.value == "")
				{
					alert("Please enter the Address");
					return false;
				}
				else if (document.aRec.City.value == "")
				{
					alert("Please enter the City");
					return false;
				}
				else if (document.aRec.State.value == "")
				{
					alert("Please enter the State");
					return false;
				}
				else if (document.aRec.Zipcode.value == "")
				{
					alert("Please enter the Zipcode");
					return false;
				}				
				else if (document.aRec.Shiptophone.value == "")
				{
					alert("Please enter the Phone Number");
					return false;
				}
				else if (document.aRec.Email.value == "")
				{
					alert("Please enter the Email Address");
					return false;
				}*/else
				{
					document.aRec.action = "default.cfm?subpageid=editrecinvoice&aSAction=submit";
					return true;
				}/*
				else
				{
					
					aUPrice = document.aRec.TransactionAmount.value;
					if (aUPrice.indexOf(".") == "-1")
					{
						aUPrice = aUPrice + ".00";
						document.aRec.TransactionAmount.value = aUPrice;
					}
					aCfm = confirm("Would you like to authorize this credit card for $"+aUPrice);
					if (aCfm)
					{
						document.aRec.Orderstring.value = "100InternetPayment"+aUPrice+"1N||";
						document.aRec.ActualCC.value = document.aRec.Accountnumber.value;
						document.aRec.ActualEM.value = document.aRec.Month.value;
						document.aRec.ActualEY.value = document.aRec.Year.value;
                                                // for testing change the www to developer
						document.aRec.action =	"https://www.skipjackic.com/scripts/EvolvCC.dll?Authorize";
						return true;
					}
					else
					{
						return false;
					}
				} 
			}*/
		}
		
function SubmitOnlyRecForm()
		{
			if (document.aRec.aReceiptDate.value == "")
				{
					alert("Please enter the Receipt Date");
					return false;					
				}
				else if (document.aRec.TransactionAmount.value == "" || document.aRec.TransactionAmount.value == "0.00" || document.aRec.TransactionAmount.value == "0")
				{
					alert("Please enter the Amount Received");
					return false;
				}
				else if (document.aRec.sjname.value == "")
				{
					alert("Please enter the Name on Card");
					return false;
				}
				else if (document.aRec.aCCType.value == "")
				{
					alert("Please select the Card Type");
					return false;
				}
				else if (document.aRec.Accountnumber.value == "")
				{
					alert("Please enter the Credit Card Number");
					return false;
				}
				else if (document.aRec.StreetAddress.value == "")
				{
					alert("Please enter the Address");
					return false;
				}
				else if (document.aRec.City.value == "")
				{
					alert("Please enter the City");
					return false;
				}
				else if (document.aRec.State.value == "")
				{
					alert("Please enter the State");
					return false;
				}
				else if (document.aRec.Zipcode.value == "")
				{
					alert("Please enter the Zipcode");
					return false;
				}				
				else if (document.aRec.Shiptophone.value == "")
				{
					alert("Please enter the Phone Number");
					return false;
				}
				else if (document.aRec.Email.value == "")
				{
					alert("Please enter the Email Address");
					return false;
				}
				else
				{
					aUPrice = document.aRec.TransactionAmount.value;
					if (aUPrice.indexOf(".") == "-1")
					{
						aUPrice = aUPrice + ".00";
						document.aRec.TransactionAmount.value = aUPrice;
					}
					document.aRec.Orderstring.value = "100InternetPayment"+aUPrice+"1N||";
					document.aRec.ActualCC.value = document.aRec.Accountnumber.value;
					document.aRec.ActualEM.value = document.aRec.Month.value;
					document.aRec.ActualEY.value = document.aRec.Year.value;
					document.aRec.action =	"default.cfm?subpageid=editrecinvoice&aSAction=submit";
					document.aRec.submit();
				}
		}

function echeck(str)
		{
			var aResult = true
			var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
			if (str != "") {
				if (filter.test(str))
					aResult=true
				else{
					alert("Please enter a valid From e-mail address")
					aResult=false
				}
			}
			return (aResult)
		}	

function CheckEmailForm()
		{
			if (document.EmailForm.aFrom.value == "")
			{
				alert("Please enter the From field");
				return false;
			}
			else if (!echeck(document.EmailForm.aFrom.value))
			{
				return false;
			}
			else if (document.EmailForm.aSubject.value == "")
			{
				alert("Please enter the Subject");
				return false;				
			}
			else if (document.EmailForm.aMessage.value == "")
			{
				alert("Please enter the Message");
				return false;				
			}
			else
			{
				return true;
			}
		}
	
function CheckCategory(aVal)
{
	if (aVal == "-1")
	{
		alert("You cannot select a Category Name. Please select a SubCategory.");
	}
}