// Set Global Variables
var theSpaces = / /g;
var theChars = /\D/g;
var theNums = /\d/g;
var newWindow;

// Swap Image
function swapImage(theAction, theImage, theOriginalImage) {
if (theAction == 1) {
	document[theImage].src = eval(theImage + "_On.src");
	}
if (theAction == 0) {
	document[theImage].src = eval(theImage + "_Off.src");
	}
if (theOriginalImage) {
	document[theOriginalImage].src = eval(theOriginalImage + "_On.src");
	}
}

// Swap Style
function swapStyle(theID, theClass) {
	theID.className = theClass;
}

// Show Layer
function showLayer(theLayer, theWidth, thePositionX, thePositionY) {
if (document.getElementById || document.all) {
	eval(theLayer + ".style.width = theWidth");
	eval(theLayer + ".style.left = thePositionX");
	eval(theLayer + ".style.top = thePositionY");
	eval(theLayer + ".style.visibility = 'visible'");
	}
}

// Hide Layer
function hideLayer(theLayer) {
if (document.getElementById || document.all) {
	eval(theLayer + ".style.visibility = 'hidden'");
	}
}

// Give Focus
function giveFocus(theForm, theField) {
	eval("document.forms(" + theForm + ")." + theField + ".focus()");
}

// Write Email
function writeEmail(theUser, theDomain) {
var theAddress = theUser + "@" + theDomain
	document.write("<A HREF=\"mailto:" + theAddress + "\">" + theAddress + "</A>");
}

// Make Remote
function makeRemote(theURL, theWidth, theHeight, theOptions) {
var theCenterWidth = (window.screen.width - theWidth) / 2;
var theCenterHeight = (window.screen.height - theHeight) / 2;
if (newWindow) {
	newWindow.close();
	}
	newWindow = window.open(theURL, "Remote", "Width=" + theWidth + ",Height=" + theHeight + "," + theOptions);
	newWindow.moveTo(theCenterWidth, theCenterHeight - 50);
	newWindow.focus();
}

// Check Zip
function checkZip(theField) {
	theField.value = theField.value.replace(theChars, "");
var theLength = theField.value.length;
var theDash = theField.value.indexOf("-");
if (theLength == 5) {
	return true;
	}
if (theLength == 9) {
	var Part1 = theField.value.substr(0,5);
	var Part2 = theField.value.substr(5,4);
	theField.value = Part1 + "-" + Part2;
	return true;
	}
}

// Check Phone
function checkPhone(theField) {
	theField.value = theField.value.replace(theChars, "");
if (!isNaN(theField.value) && theField.value.length == 10) {
	var Part1 = theField.value.substr(0,3);
	var Part2 = theField.value.substr(3,3);
	var Part3 = theField.value.substr(6,4);
	theField.value = Part1 + "-" + Part2 + "-" + Part3;
	return true;
	}
}

// Check Email
function checkEmail(theField) {
var theInvalidChars = /[^A-Za-z0-9._@]/g;
	theField.value = theField.value.replace(theInvalidChars, "");
var theLength = theField.value.length;
var theAt = theField.value.indexOf("@");
var thePeriod = theField.value.lastIndexOf(".");
if (!(theLength < 8 || theAt < 2 || (thePeriod > theLength - 3 || thePeriod < theLength - 5) || theAt > thePeriod - 3)) {
	return true;
	}
}

// Check Password
function checkPassword(theField) {
if (theField.value.length >= 8 && theField.value.length <= 20) {
	for (i = 0; i <= 9; i++) {
		if (theField.value.indexOf(i) != -1) {
			return true;
			}
		}
	}
}

// Show Error
function showError(theID, theClass, theError) {
if (document.all) {
	document.all[theID].className = theClass;
	document.all[theID].innerHTML = theError;
	}
else {
	alert(theError);
	}
	return false;
}

// Generate Image
function generateImage(theNumber) {
var theImage = "/Images/Captcha/IMG000" + theNumber + ".jpg";
	document.SecurityImage.src = theImage;
}

// Confirm Delete
function confirmDelete(theURL) {
if (confirm("Are you sure you wish to delete this item?\nThis action cannot be undone.")) {
	location.href = theURL;
	}
}

function Select_Value_Set(SelectName, Value) {
  SelectObject = document.all[SelectName];
  for(index = 0; index < SelectObject.length; index++) 
  {
     if(SelectObject[index].value == Value)
       SelectObject.selectedIndex = index;
  }
}


function openNewWindow(mypage,w,h)
{   
   h = screen.height * 0.85;
   w = screen.width * 0.95;


   winprops = "scrollbars=yes,menubar=no,locationbar=no,toolbar=no,resizable=yes,dependent=yes,alwaysRaised=yes,hotkeys=no,directories=no,status=yes," + ' height=' + h +',width=' + w;

   win = window.open(mypage,"theMailMonster",winprops);

}


function payPalOpenWindow()
{
   openNewWindow('CreditsQuickDeposit.php?countryId=' + document.all.PAYPAL_COUNTRY.value)
}



function submitFormContacts()
{

   var EmailAlias    = document.all.EmailAlias.value;
   var FirstName     = document.all.FirstName.value;
   var LastName      = document.all.LastName.value;
   var Address1      = document.all.Address1.value;
   var CityTown      = document.all.CityTown.value;
   var State         = document.all.State.value;
   var ZipPostcode   = document.all.ZipPostcode.value;
   var Country       = document.all.Country.value;

   var ErrorMsg = "";

   if(EmailAlias.length == 0)
      ErrorMsg = ErrorMsg + "- Email Alias is a Mandatory Field<BR>"

   if(FirstName.length == 0)
      ErrorMsg = ErrorMsg + "- First Name is a Mandatory Field<BR>"

   if(LastName.length == 0)
      ErrorMsg = ErrorMsg + "- Last Name is a Mandatory Field<BR>"

   if(Address1.length == 0)
      ErrorMsg = ErrorMsg + "- Address 1 is a Mandatory Field<BR>"

   if(CityTown.length == 0)
      ErrorMsg = ErrorMsg + "- City is a Mandatory Field<BR>"

   if(State.length == 0)
      ErrorMsg = ErrorMsg + "- State/Province is a Mandatory Field<BR>"

   if(ZipPostcode.length == 0)
      ErrorMsg = ErrorMsg + "- Zip Postal Code is a Mandatory Field<BR>"

   if(Country.length == 0 || Country == "Please Select")
      ErrorMsg = ErrorMsg + "- Country is a Mandatory Field<BR>"

   if (ErrorMsg.length > 0)   
   {
     document.getElementById('ErrorBox').className = 'ErrorBox';
     document.all.ErrorBox.innerHTML = ErrorMsg;
   }
   else
     document.CONTACTS_UPDATE.submit();
}



//Redirect Submission of forms
function redirectOutput(myForm)
{   
    h = screen.height * 0.85;
    w = screen.width * 0.70;

    winprops = "scrollbars=yes,menubar=no,locationbar=no,toolbar=no,resizable=yes,dependent=yes,alwaysRaised=yes,hotkeys=no,directories=no,status=yes" + 'height=' + h +',width=' + w;

   win = window.open('PreviewRedirect.php','Popup_Window',winprops);
   myForm.target = 'Popup_Window';
}


function previewLetter(filename)
{

    document.PreviewForm.FILE_NAME.value = filename;

    redirectOutput(document.PreviewForm);
    document.PreviewForm.action = "PDFContainer.php";
    document.PreviewForm.submit();

}


function tblColHand(tbl) 
{
   tbl.style.cursor='pointer';
}
