var showIntId = 0;
var hideIntId = 0;

var lowerIntId = 0;
var raiseIntId = 0;

var interval = 20;
var menuHeight = 22;
var heightRate = menuHeight/2;

var openAction = true;

var menuShowIntId = Array();
var menuHideIntId = Array();
var menuSubmenu = Array();
//menuSubmenu[1] = "1,2,3";
//menuSubmenu[2] = "";

function show(divElemId, widthRate, maxWidth, heightRate, maxHeight)
{
	var widthDone = false;
	var heightDone = false;
	var divElem = document.getElementById(divElemId);
	var divElemCurrentWidth = 0;
	divElemCurrentWidth = parseInt(divElem.style.width);
	var divElemCurrentHeight = 0;
	divElemCurrentHeight = parseInt(divElem.style.height);

	if(maxWidth > 0 && divElemCurrentWidth < maxWidth) {
		divElemCurrentWidth = divElemCurrentWidth + widthRate;
		divElem.style.width=''+divElemCurrentWidth+'px';
	} else {
		widthDone = true;
	}

	if(maxHeight > 0 && divElemCurrentHeight < maxHeight) {
		divElemCurrentHeight = divElemCurrentHeight + heightRate
		divElem.style.height=''+divElemCurrentHeight+'px'
	} else {
		heightDone = true;
	}

	if(widthDone && heightDone) {
		clearInterval(showIntId);
	}
}

function hide(divElemId, widthRate, minWidth, heightRate, minHeight)
{
	var widthDone = false;
	var heightDone = false;
	var divElem = document.getElementById(divElemId);
	var divElemCurrentWidth = 0;
	divElemCurrentWidth = parseInt(divElem.style.width);
	var divElemCurrentHeight = 0;
	divElemCurrentHeight = parseInt(divElem.style.height);

	if(minWidth > 0 && divElemCurrentWidth > minWidth) {
		divElemCurrentWidth = divElemCurrentWidth - widthRate;
		divElem.style.width=''+divElemCurrentWidth+'px';
	} else {
		widthDone = true;
	}

	if(minHeight > 0 && divElemCurrentHeight > minHeight) {
		divElemCurrentHeight = divElemCurrentHeight - heightRate
		divElem.style.height=''+divElemCurrentHeight+'px'
	} else {
		heightDone = true;
	}

	if(widthDone && heightDone) {
		clearInterval(hideIntId);
	}
}

function hideSubmenu(menuId) {
    var intId = menuShowIntId[menuId];
    if( intId != null ) {
	    clearInterval(intId);
        menuShowIntId[menuId] = null;
    }
	//clearInterval(showIntId);
	var submenuIds = menuSubmenu[menuId];
	if( submenuIds == null ) {
		return;
	}
	if( submenuIds.length == 0 ) {
		return;
	}
	var submenus = submenuIds.split(",");
	if( submenus.length > 0 ) {
		var command = "hide('menu_group_" +menuId + "', 0, 0, " + heightRate + ", " + menuHeight +")";
		intId=setInterval(command,interval);
        menuHideIntId[menuId] = intId;
		//hideIntId=setInterval(command,interval);

        var submenus = menuSubmenu[menuId].split(",");
        for ( var i=0, len=submenus.length; i<len; i++ ){
            var submenuDivElem = document.getElementById('submenu_'+submenus[i]);
            if( submenuDivElem != null ) {
                submenuDivElem.style.visibility='hidden';
            }
        }
    }
}

function showSubmenu(menuId) {
    var intId = menuHideIntId[menuId];
    if( intId != null ) {
	    clearInterval(intId);
        menuHideIntId[menuId] = null;
    }
	//clearInterval(hideIntId);
	var submenuIds = menuSubmenu[menuId];
	if( submenuIds == null ) {
		return;
	}
	if( submenuIds.length == 0 ) {
		return;
	}
	var submenus = submenuIds.split(",");

	if( submenus.length > 0 ) {
        var maxHeight = (submenus.length + 1) * menuHeight;
        var command = "show('menu_group_" +menuId + "', 0, 0, " + heightRate + ", " + maxHeight +")";
        intId=setInterval(command,interval);
        menuShowIntId[menuId] = intId;
        //showIntId=setInterval(command,interval);


        for ( var i=0, len=submenus.length; i<len; i++ ){
            var submenuDivElem = document.getElementById('submenu_'+submenus[i]);
            if( submenuDivElem != null ) {
                submenuDivElem.style.visibility='visible';
            }
        }
    }
}


function validateEnquiryForm(theForm) {
	var allRequiredSelected = true;
	var errorMessage = 'Please fill in the following required field(s):';

	if(theForm.businessName.value=='') {
		//allRequiredSelected = false;
	}

	if(theForm.contactName.value=='') {
		allRequiredSelected = false;
		errorMessage = errorMessage + '\nContact name';
	}

	if(theForm.phone.value=='') {
		allRequiredSelected = false;
		errorMessage = errorMessage + '\nPhone';
	}

	if(theForm.email.value=='') {
		allRequiredSelected = false;
		errorMessage = errorMessage + '\nE-Mail';
	} else {
		var addSymbolIndex = theForm.email.value.indexOf('@');
		var dotSymbolIndex = theForm.email.value.indexOf('.');
		var lastDotSymbolIndex = theForm.email.value.lastIndexOf('.');
		var sizeOfEmailAddress = theForm.email.value.length;
		if(addSymbolIndex <= 0 || dotSymbolIndex <= 0 || (dotSymbolIndex - addSymbolIndex) == 1 || ((theForm.email.value.length - 1) == lastDotSymbolIndex)) {
			errorMessage = errorMessage + '\nValid E-Mail';
		}
	}

	if(theForm.regarding.value=='') {
		allRequiredSelected = false;
		errorMessage = errorMessage + '\nRegarding';
	}

	if(theForm.hearUs.value=='') {
		allRequiredSelected = false;
		errorMessage = errorMessage + '\nHow did you find us?';
	}

	if(theForm.message.value=='') {
		allRequiredSelected = false;
		errorMessage = errorMessage + '\nMessage';
	}

	if(!allRequiredSelected) {
		alert(errorMessage);
	} else {
		alert('An email will be sent to your email address provided in the form above to acknowledge receiving of your enquiry.\n\nThank you for your enquiry with Knowledge Cube.');
	}

	return allRequiredSelected;
}
