var windowWidth = 0, windowHeight = 0;
var contentH = 0, menuH = 0;

function redoPadding() {
	if( typeof( window.innerWidth ) == 'number' ) {
	//Non-IE
		windowWidth = window.innerWidth;
		windowHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
			//IE 4 compatible
				windowWidth = document.body.clientWidth;
				windowHeight = document.body.clientHeight;
			}
	
	var LI = document.getElementById('main_menu').getElementsByTagName('li');
	for(var i=0; i<LI.length; i++) {
		LI[i].style.paddingLeft  = 0 + "px";
		LI[i].style.paddingRight = 0 + "px";
	}

	var LI_count = LI.length;
	var ULwidth = document.getElementById('main_menu').offsetWidth;
	var X = 0;
	for(var i=0; i<LI.length; i++) {
		X += LI[i].offsetWidth;
	}
	// alert("Ul width=" + ULwidth + "X width=" + X);
	var Y = (ULwidth - X);
	var P = ((Y / LI_count) - LI_count) / 2;
	// alert("ULwidth=" + ULwidth + ", X=" + X + "P=" + P);

	for(var i=0; i<LI.length-1; i++) {
		LI[i].style.paddingLeft  = P + "px";
		LI[i].style.paddingRight = P + "px";
	}
}

/*========= Check Delivery address Form ==================*/

function checkDeliveryForm()
{

        // instantiate object
        fv = new validateData();

        // perform checks

        // check Organisation field (Optional)
	setCookie365("b_organisation",document.Delivery.b_organisation.value);
	if (!fv.isEmpty(document.Delivery.b_organisation.value)) {
		setCookie365("b_organisation",document.Delivery.b_organisation.value,365);
	}

        // check Purchase Order field (Optional)
	setCookie365("b_PO",document.Delivery.b_PO.value);
        if (!fv.isEmpty(document.Delivery.b_PO.value))
        {
		setCookie365("b_PO",document.Delivery.b_PO.value,365);
	}

        // check First Name field (Required)
	setCookie365("b_first",document.Delivery.b_first.value);
        if (fv.isEmpty(document.Delivery.b_first.value))
        {
                fv.raiseError("Please enter your First Name");
        } else {
		if (!fv.isAlphabeticSpecial(document.Delivery.b_first.value)) {
                	fv.raiseError("First Name Invalid. This field may only contain alphabetic characters plus \- \' , ");
		} else {
			setCookie365("b_first",document.Delivery.b_first.value,365);
		}
	}

        // check Last Name field (Required)
	setCookie365("b_last",document.Delivery.b_last.value);
        if (fv.isEmpty(document.Delivery.b_last.value))
        {
                fv.raiseError("Please enter your Last Name");
        } else {
		if (!fv.isAlphabeticSpecial(document.Delivery.b_last.value)) {
                	fv.raiseError("Last Name Invalid. This field may only contain alphabetic characters plus \- \' , ");
		} else {
			setCookie365("b_last",document.Delivery.b_last.value,365);
		}
	}

        // check Email address (Required)
	setCookie365("b_email",document.Delivery.b_email.value);
        if (fv.isEmpty(document.Delivery.b_email.value))
        {
                fv.raiseError("Please enter your Email address");
        } else {
                // check for valid Email address format
                if (!fv.isEmailAddress(document.Delivery.b_email.value))
                {
                        fv.raiseError(document.Delivery.b_email.value + " - is not a valid Email address, please re-enter");
		} else {
			setCookie365("b_email",document.Delivery.b_email.value,365);
		}
        }

        // check phone (Required)
	setCookie365("b_phone",document.Delivery.b_phone.value);
        if (fv.isEmpty(document.Delivery.b_phone.value)) {
                fv.raiseError("Please enter your Phone Number");
        } else {
        	if (!fv.isNumber(document.Delivery.b_phone.value)) {
                        fv.raiseError(document.Delivery.b_phone.value + " - is not a valid Number, please re-enter");
		} else {
			setCookie365("b_phone",document.Delivery.b_phone.value,365);
		}
	}

        // check address field (Required)
	setCookie365("b_addr",document.Delivery.b_addr.value);
        if (fv.isEmpty(document.Delivery.b_addr.value))
        {
                fv.raiseError("Please enter your Address");
	} else {
		setCookie365("b_addr",document.Delivery.b_addr.value,365);
	}
        // check address field 2 
	setCookie365("b_addr2",document.Delivery.b_addr2.value);
        if (!fv.isEmpty(document.Delivery.b_addr2.value))
        { 
		setCookie365("b_addr2",document.Delivery.b_addr2.value,365);
	}


        // check city field (Required)
	setCookie365("b_city",document.Delivery.b_city.value);
        if (fv.isEmpty(document.Delivery.b_city.value))
        {
                fv.raiseError("Please enter your City");
        } else {
		if (!fv.isAlphabeticSpecial(document.Delivery.b_city.value)) {
                	fv.raiseError("City Invalid. This field may only contain alphabetic characters plus \- \' , ");
		} else {
			setCookie365("b_city",document.Delivery.b_city.value,365);
		}
	}


        // if errors, display, else proceed
        if (fv.numErrors() > 0)
        {
                fv.displayErrors();
                return false;
        }
        else
        {
                return true;
        }

}

/*========= End of Address Form check ======================*/



/*========= Product Form check ======================*/
function checkProductForm() {

        // instantiate object
        fv = new validateData();

        // perform checks

        // check Size  fields (Optional)
	var Size=new Array(); 
	Size[1] = (document.product_update_form.size_1.value);
	Size[2] = (document.product_update_form.size_2.value);
	Size[3] = (document.product_update_form.size_3.value);
	Size[4] = (document.product_update_form.size_4.value);
	Size[5] = (document.product_update_form.size_5.value);
	Size[6] = (document.product_update_form.size_6.value);
	if (fv.isEmpty(Size[1])) {
		for (i=2;i<7;i++) {
			if (!fv.isEmpty(Size[i])) {
				fv.raiseError("Field size_1 is empty but Field  size_"+i+" is NOT");
			}
		}
	}
	if (!fv.isEmpty(Size[1])) {
		for (i=2;i<7;i++) {
			if (fv.isEmpty(Size[i])) {
				fv.raiseError("Field size_1 is NOT empty but Field  size_"+i+" is");
			} else {
				break;
			}
		}
	}
		
        // check price fields 
	var Price=new Array(); 
	Price[1] = (document.product_update_form.price_1.value);
	Price[2] = (document.product_update_form.price_2.value);
	Price[3] = (document.product_update_form.price_3.value);
	Price[4] = (document.product_update_form.price_4.value);
	Price[5] = (document.product_update_form.price_5.value);
	Price[6] = (document.product_update_form.price_6.value);
	if ( fv.isEmpty(Price[1])) {
		fv.raiseError("Field  price_1 is empty");
	}
	for (i=2;i<7;i++) {
		if (!fv.isEmpty(Size[i]) && fv.isEmpty(Price[i])) {
			fv.raiseError("Field size_"+i +" is NOT empty but Field  price_"+i+" is");
		}
	}
		

        // if errors, display, else proceed
        if (fv.numErrors() > 0)
        {
                fv.displayErrors();
                return false;
        }
        else
        {
                return true;
        }

}
/*========= End of Product Form check ======================*/

/*========= Cookie functions ======================*/

function setCookie365(c_name,value,expiredays)
{
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : "; expires="+exdate.toGMTString());
}


/*========= Popup functions ======================*/
function popupC(mylink, windowname) 
{
 var width  = 620;
 var height = 420;
 var left   = (screen.width  - width)/2;
 var top    = (screen.height - height)/2;
 var params = 'width='+width+', height='+height;
 params += ', top='+top+', left='+left;
 params += ', directories=no';
 params += ', location=no';
 params += ', menubar=no';
 params += ', resizable=no';
 params += ', scrollbars=no';
 params += ', status=no';
 params += ', toolbar=no';
 newwin=window.open(mylink,windowname, params);
 if (window.focus) {newwin.focus()}
 return false;
}

function popup1(mylink, windowname) {
	if (! window.focus)return true;
	var href;
	if (typeof(mylink) == 'string')
		href=mylink;
	else
		href=mylink.href;
	window.open(href, windowname, 'width=620, height=420, scrollbars=yes, top=200, left=200, resizable=yes');
	return false;
}

function popup2(mylink, windowname) {
	if (! window.focus)return true;
	var href;
	if (typeof(mylink) == 'string')
		href=mylink;
	else
		href=mylink.href;
	window.open(href, windowname, 'width=250, height=400, scrollbars=yes, top=200, left=200, resizable=yes');
	return false;
}

/*========= end Popup functions ======================*/



/*========= Validate Data ==========================*/

function validateData()
{
	// set up array to hold error messages
	this.errorList = new Array;

	// set up object methods
	this.isEmpty = isEmpty;	
	this.isNumber = isNumber;	
	this.isAlphabetic = isAlphabetic;	
	this.isAlphabeticSpecial = isAlphabeticSpecial;	
	this.isAlphaNumeric = isAlphaNumeric;	
	this.isWithinRange = isWithinRange;	
	this.isEmailAddress = isEmailAddress;	
	this.isChecked = isChecked;	

	this.raiseError = raiseError;	
	this.numErrors = numErrors;	
	this.displayErrors = displayErrors;	
}

function isEmpty(val)
{
	//
	// check to see if input is whitespace only or empty
	//
	if (val.match(/^s+$/) || val == "")
	{
		return true;
	}
	else
	{
		return false;
	}	
}

function isNumber(val)
{
	//
	// check to see if input is number
	//
	if (isNaN(val))
	{
		return false;
	}
	else
	{
		return true;
	}	
}

function isAlphabetic(val)
{
	//
	// check to see if input is alphabetic
	//
	if (val.match(/^[a-zA-Z]+$/))
	{
		return true;
	}
	else
	{
		return false;
	}	
}

function isAlphabeticSpecial(val)
{
	//
	// check to see if input is alphabetic with spaces and some special characters allowed
	//
	if (val.match(/^[a-zA-Z' ',\-\']+$/))
	{
		return true;
	}
	else
	{
		return false;
	}	
}

function isAlphaNumeric(val)
{
	//
	// check to see if input is alphanumeric
	//
	if (val.match(/^[a-zA-Z0-9]+$/))
	{
		return true;
	}
	else
	{
		return false;
	}	
}

function isWithinRange(val, min, max)
{
	//
	// check to see if value is within range
	//
	if (val >= min && val <= max)
	{
		return true;
	}
	else
	{
		return false;
	}	
}

function isEmailAddress(val)
{
	//
	// check to see if input is a valid email address
	//
        if (val.match(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/))
	{
		return true;
	}
	else
	{
		return false;
	}	
}

function isChecked(obj)
{
	//
	// check to see if form value is checked
	//
	if (obj.checked)
	{
		return true;
	}
	else
	{
		return false;
	}	
}


function displayErrors()
{
	//	
	// display all errors
	// iterate through error array and print each item
	//
	stR = "\n";
	for (x=0; x<this.errorList.length; x++)
	{
		stR += this.errorList[x] + "\n";
	}
	alert("Error: " + stR);
}

function raiseError(msg)
{
	//
	// add an error to error list
	//
	this.errorList[this.errorList.length] = msg;
}

function numErrors()
{
	//
	// return number of errors in error array
	//
	return this.errorList.length;
}

/*========= End of validateData ====================*/

/*========= Get elements by class ==================*/
document.getElementsByClassName = function(clsName){
    var retVal = new Array();
    var elements = document.getElementsByTagName("*");
    for(var i = 0;i < elements.length;i++){
        if(elements[i].className.indexOf(" ") >= 0){
            var classes = elements[i].className.split(" ");
            for(var j = 0;j < classes.length;j++){
                if(classes[j] == clsName)
                    retVal.push(elements[i]);
            }
        }
        else if(elements[i].className == clsName)
            retVal.push(elements[i]);
    }
    return retVal;
}


function checkNewProduct() {
	return confirm('Do you really want to Add a New Product?. Click OK to Add the Product else Cancel')
}

