////////////////////////////////
// Common Functions
////////////////////////////////

function trim(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;
}

// user menu page jump
function MenuJump(){
	var strLocation = document.getElementById('UserMenu');
	if (strLocation.length > 0){
		location.href = strLocation.value;
	}
}

///////////////////////////////////////////////////
// check allm / uncheck functions for check boxes
///////////////////////////////////////////////////
function checkAll(field){
	for (i = 0; i < field.length; i++)
		field[i].checked = true ;
}

function uncheckAll(field){
	for (i = 0; i < field.length; i++)
		field[i].checked = false ;
}

////////////////////////////////
// Form Validation Functions
////////////////////////////////

///////////////// start Reg form validation //////////////////////
function CheckRegisterForm(){

	if(document.RegForm.FirstName.value == ""){
		alert("You must enter your first name to continue")
		document.RegForm.FirstName.focus();
		return false;
	}
	if(document.RegForm.LastName.value == ""){
		alert("You must enter your last name to continue")
		document.RegForm.LastName.focus();
		return false;
	}
	if(document.RegForm.Organization.value == ""){
		alert("You must enter your organization to continue, if you are not an organization, please use your full name.")
		document.RegForm.Organization.focus();
		return false;
	}
	if(document.RegForm.Email.value == ""){
		alert("You must enter your email address to continue")
		document.RegForm.Email.focus();
		return false;
	}
	if(document.RegForm.Password.value == ""){
		alert("You must enter a password to continue")
		document.RegForm.Password.focus();
		return false;
	}
	if(document.RegForm.ConfirmPassword.value == ""){
		alert("You must confirm your password to continue")
		document.RegForm.ConfirmPassword.focus();
		return false;
	}

	if (document.RegForm.Password.value !== document.RegForm.ConfirmPassword.value){
		alert("Confirm password does not match");
		return false;
	}

	if(document.RegForm.EmailCheck.value == 1){
		alert("This email address is already in use, please select a new email address, or try the password recovery option");
		document.RegForm.Email.focus();
		return false;
	}
	return true;
}

// register password validation
function ValidatePassword_old(){
	var c = document.getElementById('Password')
    if( c.value.search(/[A-Z]/) > -1 && c.value.search(/[\d]/) > -1 && c.value.length > 5 && c.value.length < 15 ) {
		document.getElementById('PasswordComplete').innerHTML = "<img src='/images/checkmark.jpg'>"
	} else {
		alert("Must be between 5 and 15 characters long, and including atleast 1 number and 1 capital letter");
		document.getElementById('PasswordComplete').innerHTML = "<img src='/images/x.jpg'>"
	}
}

function ValidatePassword(){
	var strPassword = document.getElementById('Password')
	if( strPassword.value.length >= 5 && strPassword.value.length < 15 ){
		return true;
	} else {
		alert("Password must be between 5 and 15 characters long");
		return true;
	}
}

// check passwords are the same
function ValidateConfirmPassword(){

	var strPassword = document.getElementById('Password')
	var strConfirmPassword = document.getElementById('ConfirmPassword')

	//alert(strPassword.value + " > " + strConfirmPassword.value)

	if(strPassword.value == strConfirmPassword.value){
		document.getElementById('PasswordConfirmComplete').innerHTML = "<img src='/images/checkmark.jpg'>"
	} else {
		document.getElementById('PasswordConfirmComplete').innerHTML = "<img src='/images/x.jpg'>"
		alert("your passwords do not match. please try again");
		//strConfirmPassword.focus();
		//strConfirmPassword.select();
	}
}

// register field validation
function checkRegFormField(strFieldID, strDivID){
	if(document.getElementById(strFieldID).value.length > 0){
		document.getElementById(strDivID).innerHTML = "<img src='/images/checkmark.jpg'>"
	} else {
		document.getElementById(strDivID).innerHTML = "<img src='/images/x.jpg'>"
	}
}

function UploadImagePopup(numItemID){

	var strImageDrop = document.addLogo.LogoList;
	if(strImageDrop.value == "open_fm"){
		window.open( "logo_popup.asp?type=popup&item=" + numItemID + "", "LogoManager", "status = 0, height = 400, width = 600, scrollbars = 1, resizable = 0" );
	}
}

function UploadImagePopupLink(numItemID,strQueryString){
	window.open( "logo_popup.asp?type=popup&item=" + numItemID + "&" + strQueryString + "", "LogoManager", "status = 0, height = 280, width = 600, scrollbars = 1, resizable = 0" );
}

function ImageSelectPopup(numItemID,strQueryString){
	window.open( "logo_select.asp?item=" + numItemID + "&" + strQueryString + "", "LogoSelect", "status = 0, height = 400, width = 400, scrollbars = 1, resizable = 0" );
}

function AddShippingAddressPopup(numItemID){

	var strImageDrop = document.ShippingAddress.ShippingID;
	if(strImageDrop.value == "OpenAddress"){
		window.open( "account_manager.asp?AddressType=shipping&type=popup&item=" + numItemID + "", "AccountManager", "status = 0, height = 500, width = 600, scrollbars = 1, resizable = 0" );
	}
}
function AddBillingAddressPopup(numItemID){

	var strImageDrop = document.BillingAddress.BillingID;
	if(strImageDrop.value == "OpenAddress"){
		window.open( "account_manager.asp?AddressType=billing&type=popup&item=" + numItemID + "", "AccountManager", "status = 0, height = 500, width = 600, scrollbars = 1, resizable = 0" );
	}
}
function checkQtyForm(){

	var numQty = document.getElementById('qtyInput');
	if(numQty.value == 0 || numQty.value == ""){
		alert("You must enter a Qanutity of 1 or more to continue");
		numQty.focus();
		return false;
	}
	return true;
}

//////////////////////////////////
// Chat Popup and Message Check
//////////////////////////////////
function ChatCancelMessages(){
	var xmlHttp;
	xmlHttp = GetXmlHttpObject();
	if (!xmlHttp) return false;
	var url="support/ChatFunctions.asp?action=CancelNewMessages";
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
}

function ChatSupportPopupLink(){
	window.open( "/support/chat.asp", "ChatSupport", "location = no, status = no, height = 446, width = 500, scrollbars = 0, resizable = 0" );
}

function ChatNewMessage(){
	var Message = confirm("You have a new message from RightSleeve, Click OK to launch chat.");
	if (Message){
		ChatSupportPopupLink();
	} else {
		ChatCancelMessages();
	}
}

//////////////// End Reg Form Validation //////////////////

// validate login form
function CheckLoginFrm(){

	var strEmail = document.LoginFrm.emailaddress;
	var strPass	 = document.LoginFrm.pass;

	if(strEmail.value == ""){
		alert("You must enter an email address to continue with login");
		strEmail.focus();
		return false;
	}
	if(strPass.value == ""){
		alert("You must enter a password to continue with login");
		strPass.focus();
		return false;
	}
	return true;
}

// validate add/edit address form
function ValidateAddressFrm(){

	var strNickName		= document.AddressFrm.NickName;
	var strAddress1		= document.AddressFrm.Address1;
	var strCity			= document.AddressFrm.City;
	var strProvState	= document.AddressFrm.ProvState;
	var strPostalZip	= document.AddressFrm.PostalZip;
	var strCountry		= document.AddressFrm.Country;
	var strContactName	= document.AddressFrm.ContactName;

	if(strNickName.value == ""){
		alert("please enter a Nick Name for this address");
		strNickName.focus();
		return false;
	}
	if(strAddress1.value == ""){
		alert("please enter an address");
		strAddress1.focus();
		return false;
	}
	if(strCity.value == ""){
		alert("please enter a City for this address");
		strCity.focus();
		return false;
	}
	if(strProvState.value == ""){
		alert("please select Province / State for this address");
		strProvState.focus();
		return false;
	}
	if(strPostalZip.value == ""){
		alert("please enter a Postal / Zip Code for this address");
		strPostalZip.focus();
		return false;
	}
	if(strContactName.value == ""){
		alert("please enter a Contact Name for this address");
		strContactName.focus();
		return false;
	}
	return true;
}

// validate Qty update on checkout
function checkQtyCheckOut(strForm){

	var numMinQty = strForm.minQty;
	var numQty = strForm.Qty;

	if(parseInt(numMinQty.value) > parseInt(numQty.value)){
		alert("You must enter a quantity of " + numMinQty.value + " or higher to continue");
		return false;
	}
		strForm.submit();
}


// validate 'add to cart' function
function CheckQty(){
	var numQty = document.AddToCart.Qty;
	if(numQty.value == ""){
		alert("You must enter a Quantity to continue")
		return false;
	}
	if (isNaN(numQty.value) && (numQty.value !== ".")) {
		numQty.value = numQty.value.replace(/\D/g,'');
		alert('Only numbers are allowed in Quantity');
		numQty.focus();
		return false;
	}
	return true;
}

function HideTableQty(){

	var myTableObj = document.getElementById('ContactUs');
	myTableObj.style.display = (myTableObj.style.display == '') ? 'none' : '';
}

// validate 'add to cart' function + min Qty
function CheckMinQty(numProductID){

	var numLowestQty = document.getElementById('LowestQty');
	var numCartQty = document.getElementById('Qty');
	var strDivFlag = document.getElementById('QtyMsg');

	if(numCartQty.value == ""){
		strFlag = ("This item has a minimum quantity of " + numLowestQty.value + " units. ");
		strFlag = strFlag + ("Please enter a higher quantity to continue, or <a href='javascript:void(0);' onclick='HideTableQty();' class='link'>contact us for alternate options.</a>");
		strDivFlag.innerHTML = strFlag;
		return false;
	} else {
		if(parseFloat(numCartQty.value) < parseFloat(numLowestQty.value || numCartQty.value == null )){
			strFlag = ("This item has a minimum quantity of " + numLowestQty.value + " units. ");
			strFlag = strFlag + ("Please enter a higher quantity to continue, or <a href='javascript:void(0);' onclick='HideTableQty();' class='link'>contact us for alternate options.</a>");
			strDivFlag.innerHTML = strFlag;
			return false;
		}
	}
	var numDisplayedCurrency = document.getElementById('DisplayedCurrency');
	var numUserCurrency = document.getElementById('UserCurrency');

	if (numUserCurrency.value !==""){
		if(numUserCurrency.value == 1){
			strCurrencyName = "CND";
		} else {
			strCurrencyName = "USD";
		}
		if(numUserCurrency.value !== numDisplayedCurrency.value && (numUserCurrency.value > 0)){
			alert("You can only add " + strCurrencyName + " currency items to your cart (as configured in your account settings ).");
			return false;
		}
	}
	
	return true;
}
///////////////////////
// JS Popups
///////////////////////

function RecoverPasswordPopup() {
	window.open( "/recover_password.asp", "RecoverPassword","status = 1,height = 50,width = 350,resizable = 0,top=300,left=300,screenX=300,screenY=300" )
}

// Image Popup
function ImagePopup(imageURL,strWidth,strHeight) {
    var imgWin = window.open('about:blank','imgWin','width=' + strWidth + ', height=' + strHeight + ', left=100, top=100');
    with (imgWin.document) {
        writeln('<html><head><title>Auction Picture</title>');
        writeln('<style type="text/css"><!-- body { margin: 0px; } --></style></head>');
        writeln('<body onload="self.focus();"><a href="javascript:self.close()"><img id="pic" src="' + imageURL + '" border=0 /></a></body></html>');
        close();
        }
}
///////////////////////////
// Ajax Functions
///////////////////////////

function GetXmlHttpObject() {
	var xmlHttp = null;
	if (window.XMLHttpRequest) { // Mozilla, Safari, ...
		xmlHttp = new XMLHttpRequest();
		if (xmlHttp.overrideMimeType) xmlHttp.overrideMimeType('text/xml');
	} else if (window.ActiveXObject) { // IE
		try {
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {
				alert ("Browser does not support HTTP Request")
				return false;
			}
		}
	}
	return xmlHttp;
}

///////////////////////////////////////////////////
// update logo name
function UpdateLogoName(numLogoID){

	var xmlHttp;
	xmlHttp=GetXmlHttpObject();
	if (!xmlHttp) return false;

	var strLogoName = document.getElementById("logoname_" + numLogoID)

	var url="/includes/AjaxFunctionCall.asp"
	url=url+"?action=UpdateLogoName&LogoName=" + strLogoName.value + "&LogoID=" + numLogoID;
	//xmlHttp.onreadystatechange=function() { xmlStateChange_EmailCheck( xmlHttp ); };
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
	document.getElementById("Return_" + numLogoID).innerHTML = "Update Successful!"
}
// reset return msg for logo name
function ResetLogoMsg(numLogoID){
	document.getElementById("Return_" + numLogoID).innerHTML = ""
}

///////////////////////////////////////////////////
// send inhands date
function SetDateSession(strDateFieldName){
	var xmlHttp;
	xmlHttp=GetXmlHttpObject();
	if (!xmlHttp) return false;
	var strInHandsDate = document.getElementById(strDateFieldName);
	var url="/includes/AjaxFunctionCall.asp";
	url=url+"?action=SetDateSession&InHandsDate=" + strInHandsDate.value;
	//xmlHttp.onreadystatechange=function() { ExtTest( xmlHttp ); };
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}


///////////////////////////////////////////////////////
// set last page for return function
// debug function
function QueryStringTest( xmlHttp ) {
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
		// alert(xmlHttp.responseText);
	}
}
function SetReturnPage(strCurrentPage) {
	var xmlHttp;
	xmlHttp=GetXmlHttpObject();
	if (!xmlHttp) return false;
	var url="/includes/AjaxFunctionCall.asp?action=SetLastPage&currentpage=" + strCurrentPage;
	xmlHttp.onreadystatechange=function() { 
		QueryStringTest( xmlHttp ); 
    };
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

///////////////////////////////////////////////////////
// start rating script

function SwapRatingImage(strImage){
	ImageFlip= new Image()
	ImageFlip.src = "images/ratings/" + strImage;
	var strImageOn = document.getElementById("RatingImage");
	strImageOn.src = ImageFlip.src; return true;
}

// switch image in staff profile
function ChangeProfileImage(strImage){
	ImageFlip= new Image()
	ImageFlip.src = "images/staff/" + strImage;
	var strImageOn = document.getElementById("MainImage");
	strImageOn.src = ImageFlip.src; return true;
}

// debug function
function DisplayRating( xmlHttp ){
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
		var HTMLResponse = xmlHttp.responseText;
		var strReturnRating = HTMLResponse.split("|");
		document.getElementById("RatingResult").innerHTML = strReturnRating[0];
		// send error message if user has already rated this product
		if(strReturnRating[1] == 1){
			alert("you've already rated this product.");
		}
	}
}

function RateProduct(numProductID,numRating){
	// display loading image
	document.getElementById("RatingResult").innerHTML = "<img src='images/loading.gif'>";
	var xmlHttp;
	xmlHttp=GetXmlHttpObject();
	if (!xmlHttp) return false;
	var url="/includes/AjaxFunctionCall.asp";
	url=url+"?action=RateProduct&product=" + numProductID + "&Rating=" + numRating;
	xmlHttp.onreadystatechange=function() { DisplayRating( xmlHttp ); };
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

// Check address NickName
function AddressNickName(){

	var strNickName = document.getElementById("NickName")

	var xmlHttp;
	xmlHttp=GetXmlHttpObject();
	if (!xmlHttp) return false;
	var url="/includes/AjaxFunctionCall.asp";
	url=url+"?action=CheckAddressNickName&NickName=" + strNickName.value;
	xmlHttp.onreadystatechange=function() { GetAddressNickError( xmlHttp ); };
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
}

function GetAddressNickError( xmlHttp ){
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
		var HTMLResponse = xmlHttp.responseText;

		if(HTMLResponse == 1){
			alert("the address nick name that you've selected is already in use, please select another nick name to continue.");
			document.getElementById("NickNameCheck").value = 1;
		} else {
			document.getElementById("NickNameCheck").value = 0;
		}
	}
}

// now validatwe hidden field in form

function ValidateAddressNickName(){

	if (document.getElementById("NickNameCheck").value == 1)
	{
		alert("the address nick name that you've selected is already in use, please select another nick name to continue.");
		return false;
	}
	return true;
}

//////////////////////////////////////////////////////

function echeck(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
}

// validate email address
function CheckEmailAddress(){ 
	var xmlHttp;
	xmlHttp=GetXmlHttpObject();
	if (!xmlHttp) return false;

	var strEmailAddress = document.RegForm.Email;

	if (echeck(strEmailAddress.value)==false){
		document.getElementById("EmailValidate").innerHTML = "<img src='/images/x.jpg'>";
		strEmailAddress.value=""
		//strEmailAddress.focus();
		return false;
	} else {
		var url="/includes/AjaxFunctionCall.asp"
		url=url+"?action=ValidateRegEmail&email=" + strEmailAddress.value;
		xmlHttp.onreadystatechange=function() { xmlStateChange_EmailCheck( xmlHttp ); };
		xmlHttp.open("GET",url,true)
		xmlHttp.send(null)
		document.getElementById("EmailValidate").innerHTML = "<img src='/images/loading.gif'>"
	}
}

function xmlStateChange_EmailCheck( xmlHttp ){
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
		var HTMLResponse = xmlHttp.responseText;
		var numEmailCheck = document.RegForm.EmailCheck

		var strResponseValue = HTMLResponse.split("|",2);

		document.getElementById("EmailValidate").innerHTML = "<img src='/images/x.jpg'>"
		if(strResponseValue[0] == 0){
			document.getElementById("EmailError").innerHTML = "Your email address is already in use...<br /><a href='javascript: void(0);' onclick='RecoverPasswordPopup();' class='link'>Click here to recover your password.</A>";
			numEmailCheck.value = 1
		} else if(strResponseValue[0] == 2){
			document.getElementById("EmailError").innerHTML = "You have a pending invitation!<br /><a href='?page=signin&action=resendinvite&UserID=" + strResponseValue[1] + "' class='link'>You have been pre-registered. Click here to resend your invitation email.</a>";
			numEmailCheck.value = 1
		} else {
			numEmailCheck.value = 0
			document.getElementById("EmailError").innerHTML = "";
			document.getElementById("EmailValidate").innerHTML = "<img src='/images/checkmark.jpg'>"
		}
	}
}

////////////////////////////////////////////////
// validate email address for user invites
function CheckInviteEmailAddress(numField){ 
	var xmlHttp;
	xmlHttp=GetXmlHttpObject();
	if (!xmlHttp) return false;

	var strEmailAddress = document.getElementById( numField + "_Email" );

	if (echeck(strEmailAddress.value)==false){
		document.getElementById( numField + "_EmailValidate" ).innerHTML = "<img src='/images/x.jpg'>";
		strEmailAddress.value = ""
		return false;
	} else {
		var url="/includes/AjaxFunctionCall.asp"
		url=url+"?action=ValidateInviteEmail&email=" + strEmailAddress.value;
		xmlHttp.onreadystatechange=function() { xmlStateChange_InviteEmailCheck( xmlHttp, numField ); };
		xmlHttp.open("GET",url,true)
		xmlHttp.send(null)
		document.getElementById( numField + "_EmailValidate" ).innerHTML = "<img src='/images/loading.gif'>"
	}
}

function xmlStateChange_InviteEmailCheck( xmlHttp, numField ){
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
		var HTMLResponse = xmlHttp.responseText;
		
		if (HTMLResponse == 1){
			document.getElementById( numField + "_EmailValidate" ).innerHTML = "<img src='/images/x.jpg'>";
			alert("This email address is already in use, please enter another email address to continue");
			document.getElementById("EmailCheck").value = 1;
			document.getElementById( numField + "_Email" ).value = "";
		} else {
			document.getElementById("EmailCheck").value = 0;
			document.getElementById( numField + "_EmailValidate" ).innerHTML = "<img src='/images/checkmark.jpg'>"
		}
	}
}

// now validate invite form
function InviteValidation(){
	if(document.getElementById("EmailCheck").value == 1){
		alert("One of the email addresses you chose is not valid. please correct the flagged address.");
		return false;
	}
	return true;
}

///////////////////////////////////////////////
// validate email address - admin site
function CheckEmailAddressAdmin(){ 
	var xmlHttp;
	xmlHttp=GetXmlHttpObject();
	if (!xmlHttp) return false;

	var strEmailAddress = document.RegForm.Email;

	if (echeck(strEmailAddress.value)==false){
		document.getElementById("EmailValidate").innerHTML = "<img src='/images/x.jpg'>";
		strEmailAddress.value=""
		//strEmailAddress.focus();
		return false;
	} else {
		var url="/includes/AjaxFunctionCall.asp"
		url=url+"?action=ValidateRegEmail&email=" + strEmailAddress.value;
		xmlHttp.onreadystatechange=function() { xmlStateChange_EmailCheckAdmin( xmlHttp ); };
		xmlHttp.open("GET",url,true)
		xmlHttp.send(null)
		document.getElementById("EmailValidate").innerHTML = "<img src='/images/loading.gif'>"
	}
}

function xmlStateChange_EmailCheckAdmin( xmlHttp ){
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
		var HTMLResponse = xmlHttp.responseText;
		var numEmailCheck = document.RegForm.EmailCheck

		var strResponseValue = HTMLResponse.split("|",2);

		document.getElementById("EmailValidate").innerHTML = "<img src='/images/x.jpg'>"
		if(strResponseValue[0] == 0){
			document.getElementById("EmailError").innerHTML = "Your email address is already in use...<br /><a href=\"contacts/contact_view.asp?id=" + strResponseValue[1] + "\" target='_blank'>Click here</a> to view the main contact for this user.";
			numEmailCheck.value = 1
		} else if(strResponseValue[0] == 2){
			document.getElementById("EmailError").innerHTML = "this email address already has a pending invitation!";
			numEmailCheck.value = 1
		} else {
			numEmailCheck.value = 0
			document.getElementById("EmailError").innerHTML = "";
			document.getElementById("EmailValidate").innerHTML = "<img src='/images/checkmark.jpg'>"
		}
	}
}

// validate company name
function CheckCompanyName(){ 
	var xmlHttp;
	xmlHttp=GetXmlHttpObject();
	if (!xmlHttp) return false;

	var strOrganizationAddress = document.RegForm.Organization;

	var url="/includes/AjaxFunctionCall.asp"
	url=url+"?action=ValidateCompanyName&CompanyName=" + strOrganizationAddress.value;
	xmlHttp.onreadystatechange=function() { xmlStateChange_CompanyNameCheck( xmlHttp ); };
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
	document.getElementById("OrganizationComplete").innerHTML = "<img src='/images/loading.gif'>"
}

function xmlStateChange_CompanyNameCheck( xmlHttp ){
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete") {
		var HTMLResponse = xmlHttp.responseText;
		var numEmailCheck = document.RegForm.Organization;

		document.getElementById("OrganizationComplete").innerHTML = "<img src='/images/x.jpg'>"
		if(HTMLResponse == 0){
			alert("Company name is already in use...");
			//document.RegForm.Organization.focus();
			document.RegForm.Organization.select();
			//numEmailCheck.value = 1
		} else {
			//numEmailCheck.value = 0
			document.getElementById("OrganizationComplete").innerHTML = "<img src='/images/checkmark.jpg'>"
		}
	}
}

//==============================================
// get address on fly
function DisplayAddress(strDropName,strDivName){ 
	var xmlHttp;
	xmlHttp=GetXmlHttpObject();
	if (!xmlHttp) return false;

	var numAddressID = document.getElementById(strDropName).value;

	if (numAddressID !=="OpenAddress"){
		// display loading image
		document.getElementById(strDivName).innerHTML = "<br /><center><img src='images/loading.gif' /></center>";
		// send data
		var url="/includes/AjaxFunctionCall.asp";
		url=url+"?action=DisplayAddress&AddressID=" + numAddressID;
		xmlHttp.onreadystatechange=function() { xmlStateChange_DisplayAddress( xmlHttp,strDivName ); };
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);
	}
}
// display address
function xmlStateChange_DisplayAddress( xmlHttp,strDivName ){
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
		var HTMLResponse = xmlHttp.responseText;
		document.getElementById(strDivName).innerHTML = HTMLResponse;
	}
}
//==============================================
// update qty on products list / view cart
function UpdateItemQty(strField, strReturnField, numItemID,fromTab){ 
	var xmlHttp;
	xmlHttp=GetXmlHttpObject();
	if (!xmlHttp) return false;

	var numQty = document.getElementById(strField).value;

	// send data
	var url="/includes/AjaxFunctionCall.asp";
	url=url+"?action=UpdateQty&Qty=" + numQty + "&item=" + numItemID;
	xmlHttp.onreadystatechange=function() { xmlStateChange_ChangeUnitCost( xmlHttp,strReturnField, numItemID ); };
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);

	if(fromTab == "Complete"){
		alert("Note: you may need to update the breakdown for this item in the 'View/Edit Product' section to match the new quantity.");
	}

}
// display price update
function xmlStateChange_ChangeUnitCost( xmlHttp,strDivName, numItemID ){
	if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete") {
		var HTMLResponse = xmlHttp.responseText;
		document.getElementById(strDivName).innerHTML = HTMLResponse;
		document.getElementById("ReturnMsg" + numItemID).innerHTML = "Updated!";
	}
}
// reset update msg onkeyup
function ResetQtyUpdateMsg(numItemID){
	document.getElementById("ReturnMsg" + numItemID).innerHTML = "";
}
// End Qty Update
//================================================

function HideTableCheckOut(strTableName,strImageName){

	var myTableObj = document.getElementById(strTableName);
	myTableObj.style.display = (myTableObj.style.display == '') ? 'none' : '';

	ImageFlipClose = new Image();
	ImageFlipClose.src = "images/minus.jpg";
	ImageFlipOpen = new Image();
	ImageFlipOpen.src = "images/plus.jpg";

	var strImageOn = document.getElementById(strImageName);
	strImageOn.src = (strImageOn.src == ImageFlipClose.src) ? ImageFlipOpen.src : ImageFlipClose.src;
}

//===============================================

// display image thumb nail
function DisplayThumbImage_old(numContactID){
	var strImageNameTemp = document.getElementById("LogoList");
	if(strImageNameTemp.value !=="open_fm"){
		var strImageNameTemp = strImageNameTemp.value.split("|");
		var strImageName = strImageNameTemp[2];
		if(strImageName == ""){
			document.getElementById("DisplayThumb").innerHTML = "<center>No Image Selected</center>";
		} else {
			document.getElementById("DisplayThumb").innerHTML = "<img id='SelectedImage' src='/admin/orders/artwork/" + numContactID + "/tn_" + strImageName + "' />";
		}
	}
}

function DisplayThumbImage(numContactID){
	var strImageNameTemp = document.getElementById("LogoData");
	if(strImageNameTemp.value.length > 0){
		var strImageNameTemp = strImageNameTemp.value.split("|");
		var strImageName = strImageNameTemp[2];
		if(strImageName == ""){
			document.getElementById("DisplayThumb").innerHTML = "<center>No Image Selected</center>";
		} else {
			document.getElementById("DisplayThumb").innerHTML = "<img id='SelectedImage' src='/admin/orders/artwork/" + numContactID + "/tn_" + strImageName + "' />";
		}
	} else {
		document.getElementById("DisplayThumb").innerHTML = "<center>No Image Selected</center>";
	}
}

// display/hide prov/state select box
function HideProvState(){

	var strCountryDrop = document.getElementById("Country");
	var strProvDiv = document.getElementById("HideProvState");

	if(strCountryDrop.value == "Canada" || strCountryDrop.value == "United States"){
		strProvDiv.style.display = 'inline';
	} else {
		strProvDiv.style.display = 'none';
	}
}

// auto check shipping and billing boxes when defaults are selected.
function AddressDefaultType(){

	var numIsBilling  = document.getElementById('IsBilling');
	var numIsShipping = document.getElementById('IsShipping');
	var numIsDefaultBilling  = document.getElementById('DefaultBilling');
	var numIsDefaultShipping = document.getElementById('DefaultShipping');

	// check billing
	if(numIsDefaultBilling.checked == true){
		numIsBilling.checked = true;
	}

	// check shipping
	if(numIsDefaultShipping.checked == true){
		numIsShipping.checked = true;
	}
}

function DecorateNow(){
	document.AddToCart.SendTo.value = 1;
}

function ItemBreakdownUpdate(numItemID,strReturnPage){
		window.open( "BreakdownUpdate.asp?ItemID=" + numItemID + "&ReturnPage=" + strReturnPage + "", "LogoManager", "status = 0, height = 400, width = 400, scrollbars = 1, resizable = 1" );
}
// forum popup
function ForumPopup(){
		window.open( "forum_popup.asp", "ForumPopup", "status = 0, height = 400, width = 720, scrollbars = 1, resizable = 0" );
}
// auto select currency for registration page.
function UpdateCurreny(){

	var numCountryID = document.getElementById("Region");
	var numCurrencyID = document.getElementById("Currency");

	if(numCountryID.value == 1){
		numCurrencyID.selectedIndex = 0;
	} else {
		numCurrencyID.selectedIndex = 1;
	}
}

// auto select country for registration page.
//function UpdateCountry(){

//	var numCountryID = document.getElementById("Region");
//	var numCurrencyID = document.getElementById("Currency");

//	if(numCurrencyID.value == 1){
//		numCountryID.selectedIndex = 0;
//	} else {
//		numCountryID.selectedIndex = 1;
//	}
//}



// auto select country by currency
function UpdateCountry(){

	var numCountryID = document.getElementById("Region");
	var numCurrencyID = document.getElementById("Currency");

	if(numCurrencyID.value == 1){
		numCountryID.selectedIndex = 0;
	} else if(numCurrencyID.value == 2){
		numCountryID.selectedIndex = 1;
	}
}

// view orders page approval links
function ApprovalFormPopup(strKey,numStatus){
	window.open( "approve_order.asp?key=" + strKey + "&ApprovalStatus=" + numStatus + "", "LogoSelect", "status = 0, height = 550, width = 650, scrollbars = 1, resizable = 0" );
}

function CheckCCForm(){

	var strName = document.getElementById("CardName")
	var strCardNumber = document.getElementById("CardNumber")

	if(strName.value == ""){
		alert("You must enter `Name on Card` to continue");
		return false;
	}
	if(strCardNumber.value == ""){
		alert("You must enter `Card Number` to continue");
		return false;
	}
	return true;
}

function ValidateEmailFrom(){

	var strName  = document.getElementById("QuoteName");
	var strEmail = document.getElementById("Email");

	if (strName.value == ""){
		alert("You must enter your name to continue");
		return false;
	}
	if (strEmail.value == ""){
		alert("You must enter your email address to continue");
		return false;
	}
	return true;
}

function ValidateQtyEmailFrom(){

	var strName  = document.getElementById("Name2");
	var strEmail = document.getElementById("Email2");

	if (strName.value == ""){
		alert("You must enter your name to continue");
		return false;
	}
	if (strEmail.value == ""){
		alert("You must enter your email address to continue");
		return false;
	}
	return true;
}
function ValidateHelpEmailFrom(strForm){

	var strName  = strForm.Name;
	var strEmail = strForm.Email;

	if (strName.value == ""){
		alert("You must enter your name to continue");
		return false;
	}
	if (strEmail.value == ""){
		alert("You must enter your email address to continue");
		return false;
	}
	return true;
}

function ReplyCommentPopUp(numProductID,numCommentID){
	window.open( "ReplyComment.asp?ProductID=" + numProductID + "&CommentID=" + numCommentID + "", "ReplyComment", "status = 0, height = 160, width = 400, scrollbars = 0, resizable = 0" );
}

// Wish List functions

function ToggleWishList(){
	// if ($("#WishListInput").css("display") == 'none') $("#WishListInput").slideDown();
	// else $("#WishListInput").slideUp();
	$("#WishListInput").dialog( "open" );

}
function SwapImage(strName,strImage){
	ImageFlip=new Image(); ImageFlip.src = "/images/" + strImage; var strImageOn = document.getElementById(strName); strImageOn.src = ImageFlip.src; return true;
}

function slide1sl(){
    fademi=1;
    imgarr=new Array();
    imgstr=new Array();
    linkstr=new Array();
    clslinkstr=new Array();
    
    imgarr[0]=new Image();
    imgarr[0].src='/images/homepageslides/01_redbull.jpg';
    imgstr[0]='/images/homepageslides/01_redbull.jpg';
    linkstr[0]='';
    clslinkstr[0]='';

    imgarr[1]=new Image();
    imgarr[1].src='/images/homepageslides/02_dell.jpg';
    imgstr[1]='/images/homepageslides/02_dell.jpg';
    linkstr[1]='';
    clslinkstr[1]='';

    imgarr[2]=new Image();
    imgarr[2].src='/images/homepageslides/03_testimonials.jpg';
    imgstr[2]='/images/homepageslides/03_testimonials.jpg';
    linkstr[2]='';
    clslinkstr[2]='';

    imgarr[3]=new Image();
    imgarr[3].src='/images/homepageslides/04_guinness.jpg';
    imgstr[3]='/images/homepageslides/04_guinness.jpg';
    linkstr[3]='';
    clslinkstr[3]='';

    imgarr[4]=new Image();
    imgarr[4].src='/images/homepageslides/05_disney.jpg';
    imgstr[4]='/images/homepageslides/05_disney.jpg';
    linkstr[4]='';
    clslinkstr[4]='';

    vcurr=0;
	vnext=0;
	vssdiv=null;
	stepc=20*(3000/1000);
	dif=0.00;
	op=1.00;
	dif=(1.00/stepc);
	uagent = window.navigator.userAgent.toLowerCase();
	IEB=(uagent.indexOf('msie') != -1)?true:false;
	var scompat = document.compatMode;
	if(scompat != "BackCompat"){}
	dstr1='<div id="';
	dstr2='" style="background:#fff;position:absolute;text-align:'+"center"+';width:'+900+'px;height:'+640+'px;visibility:hidden;left:0px;top:0px;padding:0px;margin:0px;overflow:hidden;">';
	dstr3='<img id="slide1img';
	dstr4='" src="';
	dstr5='" style="position:relative;left:0px;top:0px;padding:0px;margin:0px;border:0px;" alt="" border="0" usemap="#Map';
	dstr6='" />';
	dstr7='</div>';
	this.slide1dotrans=slide1dotrans;
	this.slide1initte=slide1initte;
	this.slide1initte2=slide1initte2;
	this.slide1beftrans=slide1beftrans;
	this.slide1dotransff=slide1dotransff;
}
function slide1dotrans(){
    if(IEB==true){vssdiv.filters[0].apply();}
    objc=document.getElementById('slide1d'+vcurr);
    objn=document.getElementById('slide1d'+vnext);
    objc.style.visibility="hidden";
    objn.style.visibility="visible";
    if(IEB==true){vssdiv.filters[0].play();}
    vcurr=vnext;
    vnext=vnext+1;
    if(vnext>=5){ vnext=0; }
    setTimeout('slide1dotrans()',(5000+2000));
}

function slide1dotransff(){
    op=op-dif;
    objc=document.getElementById('slide1d'+vcurr);
    objn=document.getElementById('slide1d'+vnext);
    
    if(op<(0.00)){op=0.00;}
    objc.style.opacity = op;
    objn.style.opacity = 1.00;
    if(op>(0.00)){
		setTimeout('slide1dotransff()',50);
    } else {
		objc.style.zIndex=2;
		objn.style.zIndex=3;
		setTimeout('slide1beftrans()',4000);
    }
}

function slide1beftrans(){
    vcurr=vnext;
    vnext=vnext+1;
    if(vnext>=5) { vnext=0; }
    op=1.00;
    objc=document.getElementById('slide1d'+vcurr);
    objn=document.getElementById('slide1d'+vnext);
    objc.style.visibility="visible";
    objn.style.visibility="visible";
    objc.style.zIndex=3;
    objn.style.zIndex=2;
    objc.style.opacity = 1.00;
    objn.style.opacity = 1.00;
    slide1dotransff();
}

function slide1initte2(){
    vssdiv=document.getElementById("slide1dv");
    if(5>0){
		objc=document.getElementById('slide1d'+0);
		objc.style.visibility="visible";
    }
    if(5>1){
		if((IEB==true)||(fademi==0)) {
			vcurr=0;
			vnext=1;
			setTimeout('slide1dotrans()',4000);
		} else {
			vcurr=0;
			vnext=0;
			setTimeout('slide1beftrans()',4000);
		}
	}
}
function slide1initte(){
    i=0;
    innertxt="";
    for(i=0;i<5;i++) { innertxt=innertxt+dstr1+"slide1d"+i+dstr2+linkstr[i]+dstr3+i+dstr4+imgstr[i]+dstr5+i+dstr6+clslinkstr[i]+dstr7; }
    spage=document.getElementById('slide1dv');
    spage.innerHTML=""+innertxt;
    setTimeout('slide1initte2()',4000);
}

function ToggleElement( ElementID ) {
	if ( $( "#" + ElementID ).css('display') == "none" ) {
        $("#" + ElementID).slideDown("normal");
    } else {
        $("#" + ElementID).slideUp("normal", function() {
			$("#" + ElementID).css('display','none');
	    } );
    }
}
