/*****************************************************************

	blueCart E-Commerce 2007 - (c) 2007 Bergan Blue, Australia
	THIS IS COMMERCIAL SOFTWARE. DO NOT COPY/USE/SELL.
	
	Filename			: std-functions.js (JavaScript)
	File Version		: 1.00.00
	File Summary		: JavaScript Library to handle various blueCart related functions
	
	Developed by		: Jon Bergan <jbergan@berganblue.com.au>
	Creation Date		: 12 Oct 2007
	
	Modifications		: None
	
*****************************************************************/

var curPosition = 0;
var sliding = false;
var newPosition = 0;
var adminHelpBarToggle = 0;

function showCCVHelpBox()
{
	helpWindow = window.open(bluecartPublicRoot+"templates/layouts/templates/ccv-help-window.htm","helpWindow","status=0,toolbar=0,location=0,menubar=0,directories=0,resizable=0,scrollbars=0,width=400,height=310");

	helpWindow.moveTo(((screen.width/2)-200),((screen.height/2)-200));
}

function showPaymentOptionFields()
{
	for (var i=0; i<document.forms.paymentDetails.elements['paymentMethod'].length; i++)
		if (document.forms.paymentDetails.elements['paymentMethod'][i].checked == true)
			getContent('ajaxPaymentOptions', 'ajaxLoaderPaymentOptions', 'ajax.php?command=paymentOptionsFields&id='+document.forms.paymentDetails.elements['paymentMethod'][i].value);

	if (!document.forms.paymentDetails.elements['paymentMethod'].length)
		if (document.forms.paymentDetails.elements['paymentMethod'].checked == true)
			getContent('ajaxPaymentOptions', 'ajaxLoaderPaymentOptions', 'ajax.php?command=paymentOptionsFields&id='+document.forms.paymentDetails.elements['paymentMethod'].value);
}

function resetDeliveryAddressFields()
{
	document.forms.deliveryAddress.reset(); 

	updateStateField(-1, 'orderShippingState', 'orderShippingCountry', 'selectBox', 'inputTextbox', 'ajaxLoaderShipping', 'ajaxCheckoutShipping');
}

function newWindow (ajaxURL)
{
	hideWindowSmall();
	hideWindow();
	hideShade();

	if (screen.height > 900) document.getElementById('windowDiv').style.top = '75px';
	showShade();
	showWindow();
	updateAjaxPanel("ajaxWindow", ajaxURL);
}

function newSmallWindow (ajaxURL)
{
	hideWindowSmall();
	hideWindow();
	hideShade();

	if (screen.height > 900) document.getElementById('windowDivSmall').style.top = '75px';
	showShade();
	showWindowSmall();
	updateAjaxPanel("ajaxWindowSmall", ajaxURL);
}

function deleteWindow ()
{
	hideWindow();
	hideShade();

	var objDiv1 = document.getElementById("ajaxImageManager");
	if (objDiv1)
		displayImageManager();

	var objDiv2 = document.getElementById("ajaxOptionsManager");
	if (objDiv2)
		displayOptionsManager();
}

function deleteWindowSmall ()
{
	hideWindowSmall();
	hideShade();

	var objDiv1 = document.getElementById("ajaxImageManager");
	if (objDiv1)
		displayImageManager();

	var objDiv2 = document.getElementById("ajaxOptionsManager");
	if (objDiv2)
		displayOptionsManager();
}

function showWindowSmall()
{
	win = document.getElementById('windowDivSmall');
	win.style.display = "block";
	win.style.visibility = 'visible';
}

function hideWindowSmall()
{
	emptyAjaxPanel("ajaxWindowSmall");
	win = document.getElementById('windowDivSmall');
	win.style.display = "none";
	win.style.visibility = 'hidden';
}

function showWindow()
{
	win = document.getElementById('windowDiv');
	win.style.display = "block";
	win.style.visibility = 'visible';
}

function hideWindow()
{
	emptyAjaxPanel("ajaxWindow");
	win = document.getElementById('windowDiv');
	win.style.display = "none";
	win.style.visibility = 'hidden';
}

function hideShade()
{
	shadewrp = document.getElementById('shadeWrap');
	shade = document.getElementById('shadeLayer');
	document.body.style.overflow = 'auto';
	document.body.scroll = 'yes';
	
	shade.style.display = 'none';
	shade.style.visibility = 'hidden';
	shadewrp.style.display = 'none';
	shadewrp.style.visibility = 'hidden';
}

function showShade()
{
	shadewrp = document.getElementById('shadeWrap');
	shade = document.getElementById('shadeLayer');

	scroll(0,0);
	document.body.style.overflow = 'hidden';
	document.body.scroll = 'no';
	
	shadewrp.style.display = 'block';
	shadewrp.style.visibility = 'visible';
	shade.style.display = 'block';
	shade.style.visibility = 'visible';
}

function createPostSlug(srcId, destId)
{
    var headline_input = document.getElementById(srcId).value;

    var post_slug_array = headline_input.split(" - ");
    var post_slug = post_slug_array.join(" ");
	var post_slug_array = post_slug.split("-");
    var post_slug = post_slug_array.join("");

	var post_slug_array = post_slug.split(" ");
    var post_slug = post_slug_array.join("-");

	var post_slug_array = post_slug.split("'");
    var post_slug = post_slug_array.join("");
	var post_slug_array = post_slug.split("!");
    var post_slug = post_slug_array.join("");
    var post_slug_array = post_slug.split("@");
    var post_slug = post_slug_array.join("");
    var post_slug_array = post_slug.split("@");
    var post_slug = post_slug_array.join("");
    var post_slug_array = post_slug.split("#");
    var post_slug = post_slug_array.join("");
    var post_slug_array = post_slug.split("$");
    var post_slug = post_slug_array.join("");
    var post_slug_array = post_slug.split("%");
    var post_slug = post_slug_array.join("");
    var post_slug_array = post_slug.split("^");
    var post_slug = post_slug_array.join("");
    var post_slug_array = post_slug.split("&");
    var post_slug = post_slug_array.join("and");
    var post_slug_array = post_slug.split("*");
    var post_slug = post_slug_array.join("");
    var post_slug_array = post_slug.split("(");
    var post_slug = post_slug_array.join("");
    var post_slug_array = post_slug.split(")");
    var post_slug = post_slug_array.join("");

	var post_slug_array = post_slug.split(".");
    var post_slug = post_slug_array.join("-");
    var post_slug_array = post_slug.split("_");
    var post_slug = post_slug_array.join("-");
    var post_slug_array = post_slug.split("+");
    var post_slug = post_slug_array.join("-");

	document.getElementById(destId).value = post_slug.toLowerCase();
}

function disableAdminField(fieldId, fieldType)
{
	if (!fieldType)
		var fieldType = 'inputTextbox';

	e = document.getElementById(fieldId);
	e.disabled = true;
	e.setAttribute("class", fieldType+"Disabled");
	e.setAttribute("className", fieldType+"Disabled");
}

function enableAdminField(fieldId, fieldType)
{
	if (!fieldType)
		var fieldType = 'inputTextbox';
		
	e = document.getElementById(fieldId);
	e.disabled = false;
	e.setAttribute("class", fieldType);
	e.setAttribute("className", fieldType);
}

function checkOptions(fieldId, changeId, fieldType, fieldClear)
{
	if (!fieldType)
		var fieldType = 'inputTextbox';

	if (document.getElementById(fieldId))
	{
		if (document.getElementById(changeId))
		{
			e = document.getElementById(fieldId);
			c = document.getElementById(changeId);

			if (fieldClear == true)
				c.value = "";
		
			if (e.options[0].selected)
				disableAdminField(changeId, fieldType);
			else 
				enableAdminField(changeId, fieldType);
		}
	}
}

function checkFinalOption(fieldId, changeId, fieldType, fieldClear)
{
	if (!fieldType)
		var fieldType = 'inputTextbox';

	if (document.getElementById(fieldId))
	{
		if (document.getElementById(changeId))
		{
			e = document.getElementById(fieldId);
			c = document.getElementById(changeId);

			if (fieldClear == true)
				c.value = "";
		
			if (e.options[(e.options.length-1)].selected)
				enableAdminField(changeId, fieldType);
			else 
				disableAdminField(changeId, fieldType);
		}
	}
}

function checkSpecificOption(fieldId, changeId, optionId, fieldType)
{
	if (!fieldType)
		var fieldType = 'inputTextbox';

	e = document.getElementById(fieldId);
	
	if (e.options[optionId].selected)
		enableAdminField(changeId, fieldType);
	else 
		disableAdminField(changeId, fieldType);
}

function slideTo(section)
{ 
	if (section == 'adminHelpBarOpen') { newPosition = 250; } 
	if (section == 'adminHelpBarClose') { newPosition = 20; }
	
	slideInterval = setInterval('slide(newPosition)',20);
	return;
}

function slide(newPosition)
{
	speed = (newPosition - curPosition)*0.2;
	if (newPosition < curPosition) // rolling up
	{
		document.getElementById('contentBarHelp').style.overflow = 'hidden';
		if (curPosition + speed - 1 <= newPosition) { clearInterval(slideInterval); sliding = false; 		updateAjaxPanelStr('contentBarHelp', '<a href="javascript:toggleAdminHelpBar();"><span>&nbsp;&nbsp;<strong>Need help?</strong> Click here for assistance.</span></a>'); return; }
	} else { // rolling down
		if (curPosition + speed + 1 >= newPosition) { clearInterval(slideInterval); sliding = false; document.getElementById('contentBarHelp').style.overflow = 'auto'; return; }
	}
	curPosition += speed;

	document.getElementById('contentBarHelp').style.height = curPosition +'px';
}

function toggleAdminHelpBar()
{
	if (adminHelpBarToggle == 0)
	{
		adminHelpBarToggle = 1;
		updateAjaxPanelStr('contentBarHelp', '<a href="javascript:toggleAdminHelpBar();"><span class="underLined">&nbsp;&nbsp;<strong>Administration Panel Help</strong> -- Click here to close this tab</span></a>'+pageHelpText);
		slideTo('adminHelpBarOpen');
	} else {
		adminHelpBarToggle = 0;
		slideTo('adminHelpBarClose');
	}
}

function toggleFactorCheckbox(id)
{
	var newSetting = 0;
	
	if (document.getElementById('dummyField'+id).checked == true)
	{
		newSetting = 1;
		enableAdminField('sFactorCost'+id, 'inputTextboxSml');
	} else {
		disableAdminField('sFactorCost'+id, 'inputTextboxSml');
	}

	document.getElementById('sFactorEnabled'+id).value = newSetting;
}

function toggleFactorCheckByTextbox(id)
{
	var textVal = new String(document.getElementById('sFactorCost'+id).value);
	
	textVal = textVal.replace(/\$/,"");	
	textVal = textVal.replace(/\(/,"");	
	textVal = textVal.replace(/\)/,"");	
	
	if (textVal > 0)
	{
		document.getElementById('sFactorEnabled'+id).value = 1;
		document.getElementById('dummyField'+id).checked = true;
	} else {
		document.getElementById('sFactorEnabled'+id).value = 0;
		document.getElementById('dummyField'+id).checked = false;
	}
}

function updateShippingValueType()
{
	var e = document.getElementById('shippingValueType');

	if (e.value == "POSTCODE")
	{
		document.getElementById('POSTCODE').style.display = "block";
		document.getElementById('POSTCODE').style.visibility = "visible";
		document.getElementById('STATE').style.display = "none";
		document.getElementById('STATE').style.visibility = "hidden";
		document.getElementById('COUNTRY').style.display = "none";
		document.getElementById('COUNTRY').style.visibility = "hidden";
		document.getElementById('shippingValueCriteria1').disabled = false;
		document.getElementById('shippingValueCriteria2').disabled = true;

		if (document.getElementById('shippingValueCriteria3'))
			document.getElementById('shippingValueCriteria3').disabled = true;
			
		document.getElementById('shippingValueCriteria4').disabled = true;

	} else if (e.value == "STATE") 
	{
		document.getElementById('STATE').style.display = "block";
		document.getElementById('STATE').style.visibility = "visible";
		document.getElementById('COUNTRY').style.display = "none";
		document.getElementById('COUNTRY').style.visibility = "hidden";
		document.getElementById('POSTCODE').style.display = "none";
		document.getElementById('POSTCODE').style.visibility = "hidden";
		document.getElementById('shippingValueCriteria1').disabled = true;
		document.getElementById('shippingValueCriteria2').disabled = false;

		if (document.getElementById('shippingValueCriteria3'))
			document.getElementById('shippingValueCriteria3').disabled = false;

		document.getElementById('shippingValueCriteria4').disabled = true;

	} else if (e.value == "COUNTRY")
	{
		document.getElementById('COUNTRY').style.display = "block";
		document.getElementById('COUNTRY').style.visibility = "visible";
		document.getElementById('POSTCODE').style.display = "none";
		document.getElementById('POSTCODE').style.visibility = "hidden";
		document.getElementById('STATE').style.display = "none";
		document.getElementById('STATE').style.visibility = "hidden";
		document.getElementById('shippingValueCriteria1').disabled = true;
		document.getElementById('shippingValueCriteria2').disabled = true;

		if (document.getElementById('shippingValueCriteria3'))
			document.getElementById('shippingValueCriteria3').disabled = true;
			
		document.getElementById('shippingValueCriteria4').disabled = false;
	}
}

function updateStateField(userid, fieldName, fieldCountry, selectClass, inputClass, ajaxLoader, ajaxContent)
{
	getContent(ajaxContent, ajaxLoader, 'ajax.php?command=ajaxShowStateOptions&class='+selectClass+'&inputclass='+inputClass+'&name='+fieldName+'&id='+userid+'&relatedId='+document.getElementById(fieldCountry).value);
}

function updateStateFieldById(id, fieldName, fieldCountry, selectClass, inputClass, ajaxLoader, ajaxContent)
{
	getContent(ajaxContent, ajaxLoader, 'ajax.php?command=ajaxShowStateOptionsById&class='+selectClass+'&inputclass='+inputClass+'&name='+fieldName+'&id='+id+'&relatedId='+document.getElementById(fieldCountry).value);
}

function updateStateFieldByName(sName, fieldName, fieldCountry, selectClass, inputClass, ajaxLoader, ajaxContent)
{
	getContent(ajaxContent, ajaxLoader, 'ajax.php?command=ajaxShowStateOptionsByName&class='+selectClass+'&inputclass='+inputClass+'&name='+fieldName+'&sName='+sName+'&relatedId='+document.getElementById(fieldCountry).value);
}

function printInvoice(oid)
{
	printWindow = window.open(bluecartPublicRoot+'admin/admin.php?command=print&page=invoice&id='+oid,"printWindow","status=0,toolbar=0,location=0,menubar=0,directories=0,resizable=0,scrollbars=1,width=800,height=700");
	printWindow.screenX = ( screen.width / 2 ) - 400;
	printWindow.screenY = ( screen.height / 2 ) - 350;
}

function printPackingSlip(oid)
{
	printWindow = window.open(bluecartPublicRoot+'admin/admin.php?command=print&page=packingslip&id='+oid,"printWindow","status=0,toolbar=0,location=0,menubar=0,directories=0,resizable=0,scrollbars=1,width=800,height=700");
	printWindow.screenX = ( screen.width / 2 ) - 400;
	printWindow.screenY = ( screen.height / 2 ) - 350;
}

function toggleShippingAddressDetails()
{
	e = document.getElementById('shipping-address-details');
	
	if (e.style.visibility == "hidden" || !e.style.visibility)
	{
		e.style.visibility = "visible";
		e.style.display = "block";
	} else {
		e.style.visibility = "hidden";
		e.style.display = "none";
	}
}

function populateDeliveryAddressForm()
{
	var id = document.getElementById('addressbookEntry').value;
	
	if (id > 0)
	{
		document.getElementById('orderShippingAddressLine1').value = addressBook[id][0];
		document.getElementById('orderShippingAddressLine2').value = addressBook[id][1];
		document.getElementById('orderShippingCity').value = addressBook[id][2];
		document.getElementById('orderShippingPostcode').value = addressBook[id][4];
		document.getElementById('orderShippingCountry').value = addressBook[id][5];
	
		updateStateFieldById(id, 'orderShippingState', 'orderShippingCountry', 'selectBox', 'inputTextbox', 'ajaxLoaderShipping', 'ajaxCheckoutShipping');
	}
}

function populateDefaultDeliveryAddress()
{
	if (addressBookDefault > 0)
	{
		var id = addressBookDefault;

		if (document.getElementById('orderShippingAddressLine1').value == "" && document.getElementById('orderShippingCity').value == "" && document.getElementById('orderShippingPostcode').value == "")
		{
			document.getElementById('orderShippingAddressLine1').value = addressBook[id][0];
			document.getElementById('orderShippingAddressLine2').value = addressBook[id][1];
			document.getElementById('orderShippingCity').value = addressBook[id][2];
			document.getElementById('orderShippingPostcode').value = addressBook[id][4];
			document.getElementById('orderShippingCountry').value = addressBook[id][5];

			updateStateFieldById(id, 'orderShippingState', 'orderShippingCountry', 'selectBox', 'inputTextbox', 'ajaxLoaderShipping', 'ajaxCheckoutShipping');
		}
	}
}
