var Common = {
	activePrimaryLink : null,
	accordionStepHeight : 5,
	accordionShowTimeInSeconds : 0.1,	//This is the total amount of time in seconds it should take for a AccordionLI to open up
	accordionShowThreshold : 10,		//This is the minimum step height in pixels while opening an AccordionLI
	accordionHideTimeInSeconds : 0.01,	//This is the total amount of time in seconds it should take for a AccordionLI to close
	accordionHideThreshold : 50,		//This is the minimum step height in pixels while closing an AccordionLI
	
	init : function(){
		//FOR ACCORDIONULS
		var thisLoc = location.href + "";
		var lastIndex = thisLoc.lastIndexOf("#");
		var theDivToShowName = null;
		if( lastIndex != -1 ){
			theDivToShowName = thisLoc.substring(lastIndex + 1);
			if(theDivToShowName == ""){
				theDivToShowName = null;
			}
		}
		var foundTheDivToShowName = false;
		//~~FOR ACCORDIONULS
	
		//COMMON
		var bodyElem = document.getElementsByTagName("body")[0];
		//~~COMMON
		
		//FOR promoBlockBusinessEnquiryForm
		var promoBlockBusinessEnquiryForm = document.getElementById('promoBlockBusinessEnquiryForm');
		if(promoBlockBusinessEnquiryForm){
			promoBlockBusinessEnquiryForm.onsubmit = Common.handleEnquiryForm;
		}
		//~~FOR promoBlockBusinessEnquiryForm

		//FOR bulkDownloadForm
		var bulkDownloadForm = document.getElementById('bulkDownloadForm');
		if(bulkDownloadForm){
			Common._bulkDownloadFormSubmitter = document.getElementById('bulkDownloadFormSubmitter');
			if(Common._bulkDownloadFormSubmitter){
				Common._bulkDownloadFormErrorP = Common._bulkDownloadFormSubmitter.parentNode.nextSibling;
				if(Common._bulkDownloadFormErrorP.nodeName != "P"){
					Common._bulkDownloadFormErrorP = Common._bulkDownloadFormErrorP.nextSibling;
				}
				bulkDownloadForm.onsubmit = Common.handleBulkDownloadForm;
			}
		}
		//~~FOR bulkDownloadForm
		
		//FOR PRIMARY LINKS
		var primaryLinksUL = document.getElementById("primaryLinksUL");
		if(primaryLinksUL){
			var primaryLinksLIs = primaryLinksUL.childNodes;
			for(var i = 0 ; i < primaryLinksLIs.length ; i++){
				if( primaryLinksLIs[i].nodeName == "LI" ){
					var primaryLinkLI = primaryLinksLIs[i];
					var primaryLink = primaryLinkLI.firstChild;
					if(primaryLink.nodeName != "A"){
						primaryLink = primaryLink.nextSibling;
					}
					if( Main.hasClass(primaryLink, "activePrimaryLink") ){
						Common.activePrimaryLink = primaryLink;
					}
					var associatedDiv = null;
					if(primaryLink.nextSibling){
						associatedDiv = primaryLink.nextSibling;
						if(associatedDiv.nodeName != "DIV"){
							if(associatedDiv.nextSibling){
								associatedDiv = associatedDiv.nextSibling;
							}
						}
						if(associatedDiv.nodeName && associatedDiv.nodeName == "DIV"){
							associatedDiv._associatedLink = primaryLink;
							associatedDiv.onmouseover = Common.associatedDivHoverHandler;
							associatedDiv.onfocus = Common.associatedDivHoverHandler;
							associatedDiv.onmouseout = Common.associatedDivHoverHandlerRemover;
							associatedDiv.onblur = Common.associatedDivHoverHandlerRemover;
						}
						else{
							associatedDiv = null;
						}
					}
					primaryLink._associatedDiv = associatedDiv;
					primaryLink.onmouseover = Common.primaryLinkHoverHandler;
					primaryLink.onfocus = Common.primaryLinkHoverHandler;
					primaryLink.onmouseout = Common.primaryLinkHoverHandlerRemover;
					primaryLink.onblur = Common.primaryLinkHoverHandlerRemover;
				}
			}
		}
		//~~FOR PRIMARY LINKS
		
		
		//FOR FADE IN FADE OUTS
		var fadeInFadeOutULs = Main.getElementsByClass(bodyElem, "fadeInFadeOut");
		for(var i = 0 ; i < fadeInFadeOutULs.length ; i++){
			var fifoUL = fadeInFadeOutULs[i];
			fifoUL._children = Array();
			var lis = fifoUL.childNodes;
			for(var j = 0 ; j < lis.length ; j++){
				if(lis[j].nodeName == "LI"){
					var li = lis[j];
					fifoUL._children[fifoUL._children.length] = li;
					li._originalHeight = li.scrollHeight;
					li.style.opacity = 0;
					li.style.filter = "opacity(alpha=0)";
					li.style.zoom = "1";
					li.style.display = "none";
				}
			}
		}
		if(fadeInFadeOutULs.length > 0){
			for(var i = 0 ; i < fadeInFadeOutULs.length ; i++){
				var x = Math.floor(Math.random() * fadeInFadeOutULs[i]._children.length);
				fadeInFadeOutULs[i]._current = x;
				Common.animateFadeInFadeOutUL( fadeInFadeOutULs[i] );
			}
		}
		//~~FOR FADE IN FADE OUTS
		
		
		//FOR ACCORDIONULS
		var accordionULs = Main.getElementsByClass(bodyElem, "accordionUL");
		for(var i = 0 ; i < accordionULs.length ; i++){
			var accordionUL = accordionULs[i];
			var accordionLIs = accordionUL.childNodes;
			for(var j = 0 ; j < accordionLIs.length ; j++){
				var accordionLI = accordionLIs[j];
				if(accordionLI.nodeName == "LI"){
					accordionLI._originalHeight = accordionLI.offsetHeight;
					var h3 = accordionLI.firstChild;
					if(h3.nodeName != "H3"){
						h3 = h3.nextSibling;
					}
					h3.parentNode._uptoHeight = h3.offsetHeight;
					h3.parentNode.style.height = h3.parentNode._uptoHeight + "px";
					h3.parentNode.style.overflow = "hidden";
					var input = null;
					if( h3.getElementsByTagName("input") ){
						input = h3.getElementsByTagName("input")[0];
					}
					if(input){
						input.style.cursor = "default";
					}
					var span = h3.getElementsByTagName("span")[0];
					
					if(span.nodeName != "SPAN"){
						span = span.nextSibling;
					}
					if( ! Main.hasClass(span, "noBackgroundImage") ){
						span.style.backgroundImage = "url(/images/SliderDown.jpg)";
					}
					span.onclick = Common.showHideAccordionDiv;
					span.style.cursor = "pointer";
					h3._child = span;
					var descDiv = h3.nextSibling;
					if(descDiv.nodeName != "DIV"){
						descDiv = descDiv.nextSibling;
					}
					descDiv.parentNode._descDiv = descDiv;
					
					if(theDivToShowName && theDivToShowName == h3.id){
						foundTheDivToShowName = true;
						Common.showHideAccordionDiv2(h3);
					}
				}
			}
		}

		//if( theDivToShowName && (!foundTheDivToShowName) ){
		if(accordionULs.length > 0){
			if( theDivToShowName ){
				if(location.hash.length > 1){
					location.hash = "#";
					//This below fix for Safari 3
					//From here
					if(location.hash.length > 1){
						location.hash = "# ";
					}
					//Upto here
				}
			}
		}
		//~~FOR ACCORDIONULS
	},
	
	
	//ALL PURPOSE
	trim : function(stringToTrim){
		return stringToTrim.replace(/^\s+|\s+$/g,"");
	},

	//ALL PURPOSE
	ltrim : function(stringToTrim){
		return stringToTrim.replace(/^\s+/,"");
	},

	//ALL PURPOSE
	rtrim : function(stringToTrim){
		return stringToTrim.replace(/\s+$/,"");
	},
	
	//ALL PURPOSE
	removeAllSpaces : function(stringToTrim){
		return stringToTrim.replace(/\s+/g,"");
	},

	
	//FOR PRIMARY LINKS
	primaryLinkHoverHandler : function(){
		var elem = this;
		if(elem){	//to handle null value
			//setTimeout( function(){ Common.associatedDivShower(elem); }, 50);
			//setTimeout( function(){ Common.associatedDivShower(elem); }, 5);
			Common.associatedDivShower(elem);
		}
	},
	
	associatedDivShower : function(elem){
		if(elem != Common.activePrimaryLink){
			Main.addClass(elem, "activePrimaryLink");
			if(Common.activePrimaryLink){
				Main.removeClass(Common.activePrimaryLink, "activePrimaryLink");
			}
			if(elem._associatedDiv){
				Main.removeClass(elem._associatedDiv, "hideBlock");
			}
			if(Common.activePrimaryLink){
				if(Common.activePrimaryLink._associatedDiv){
					Main.addClass(Common.activePrimaryLink._associatedDiv, "hideBlock");
				}
			}
		}
	},
	
	primaryLinkHoverHandlerRemover : function(){
		var elem = this;
		if(elem){	//to handle null value
			//elem._removeTimer = setTimeout( function(){ Common.associatedDivHider(elem); }, 50);
			//elem._removeTimer = setTimeout( function(){ Common.associatedDivHider(elem); }, 5);
			Common.associatedDivHider(elem);
		}
	},

	associatedDivHider : function(elem){
		if(elem != Common.activePrimaryLink){
			Main.removeClass(elem, "activePrimaryLink");
			if(Common.activePrimaryLink){
				Main.addClass(Common.activePrimaryLink, "activePrimaryLink");
			}
			if(elem._associatedDiv){
				Main.addClass(elem._associatedDiv, "hideBlock");
			}
			if(Common.activePrimaryLink){
				if(Common.activePrimaryLink._associatedDiv){			
					Main.removeClass(Common.activePrimaryLink._associatedDiv, "hideBlock");
				}
			}
		}
	},	

	associatedDivHoverHandler : function(){
		var elem = this._associatedLink;
		clearTimeout(this._associatedLink._removeTimer);	//Doesn't work if we use elem in place of this
		//setTimeout( function(){ Common.associatedDivShower(elem); }, 50);
		//setTimeout( function(){ Common.associatedDivShower(elem); }, 5);
		Common.associatedDivShower(elem);
	},

	associatedDivHoverHandlerRemover : function(){
		var elem = this._associatedLink;
		//elem._removeTimer = setTimeout( function(){ Common.associatedDivHider(elem); }, 50);
		//elem._removeTimer = setTimeout( function(){ Common.associatedDivHider(elem); }, 5);
		Common.associatedDivHider(elem);
	},
	//~~FOR PRIMARY LINKS
	
	//FOR FADE IN FADE OUTS
	animateFadeInFadeOutUL : function(theUL){
		if(theUL._current >= theUL._children.length){
			theUL._current = 0;
		}
		theUL._children[theUL._current].style.display = "block";
		Common.slowShowFaderDiv(theUL, theUL._children[theUL._current], 0);
	},

	slowShowFaderDiv : function(container, elem, opacity){
		elem.style.opacity = opacity / 100;
		elem.style.filter = "alpha(opacity=" + opacity + ")";
		if(opacity < 100){
			setTimeout(function(){Common.slowShowFaderDiv(container, elem, opacity+1);}, 50);
		}
		else{
			setTimeout(function(){Common.slowHideFaderDiv(container, elem, 80);}, 5000);
		}
	},

	slowHideFaderDiv : function(container, elem, opacity){
		elem.style.opacity = opacity / 100;
		elem.style.filter = "alpha(opacity=" + opacity + ")";
		if(opacity > 0){
			setTimeout(function(){Common.slowHideFaderDiv(container, elem, opacity-1);}, 1);
		}
		else{
			elem.style.display = "none";
			container._current = container._current + 1;
			Common.animateFadeInFadeOutUL(container);
		}
	},
	//~~FOR FADE IN FADE OUTS
	
	//FOR ACCORDIONULS
	showHideAccordionDiv : function(){
		var theH3 = this.parentNode;
		Common.showHideAccordionDiv2(theH3);
	},
	
	showHideAccordionDiv2 : function(theH3){
		if(theH3.showingDiv){
			clearTimeout(theH3.parentNode._showAccordionDivTimer);
			theH3.showingDiv = false;
			if( ! Main.hasClass(theH3._child, "noBackgroundImage") ){
				theH3._child.style.backgroundImage = "url(/images/SliderDown.jpg)";
			}
			var currHeight = theH3.parentNode.offsetHeight;
			if(currHeight > theH3.parentNode._uptoHeight){
				var step = (currHeight - theH3.parentNode._uptoHeight) / Common.accordionStepHeight;
				var timer = ( Common.accordionHideTimeInSeconds * 1000) / Common.accordionStepHeight;
				if(step < Common.accordionHideThreshold){
					var factor = 0;
					factor = Common.accordionHideThreshold / step;
					step = Common.accordionHideThreshold;
					timer = timer * factor;
				}
				//alert(step + " : " + timer);
				Common.slowHideAccordionDIV(theH3.parentNode._descDiv, step, timer);
			}
		}
		else{
			clearTimeout(theH3.parentNode._hideAccordionDivTimer);
			theH3.showingDiv = true;
			if( ! Main.hasClass(theH3._child, "noBackgroundImage") ){
				theH3._child.style.backgroundImage = "url(/images/SliderUp.jpg)";
			}
			var currHeight = theH3.parentNode.offsetHeight;
			if(currHeight < theH3.parentNode._originalHeight){
				var step = (theH3.parentNode._originalHeight - currHeight) / Common.accordionStepHeight;
				var timer = ( Common.accordionShowTimeInSeconds * 1000) / Common.accordionStepHeight;
				if(step < Common.accordionShowThreshold){
					var factor = 0;
					factor = Common.accordionShowThreshold / step;
					step = Common.accordionShowThreshold;
					timer = timer * factor;
				}
				//alert(step + " : " + timer);
				Common.slowShowAccordionDIV(theH3.parentNode._descDiv, step, timer);
			}
		}
	},
	
	slowShowAccordionDIV : function(theDiv, step, timer){
		var currHeight = theDiv.parentNode.offsetHeight;
		if(currHeight < theDiv.parentNode._originalHeight){
			//var tempHeight = (theDiv.parentNode.offsetHeight + Common.accordionStepHeight);
			var tempHeight = (theDiv.parentNode.offsetHeight + step);
			if(tempHeight > theDiv.parentNode._originalHeight){
				tempHeight = theDiv.parentNode._originalHeight;
			}
			theDiv.parentNode.style.height = tempHeight + "px";
			theDiv.parentNode._showAccordionDivTimer = setTimeout( function(){ Common.slowShowAccordionDIV(theDiv, step, timer) }, timer );
		}
	},
	
	slowHideAccordionDIV : function(theDiv, step, timer){
		var currHeight = theDiv.parentNode.offsetHeight;
		if(currHeight > theDiv.parentNode._uptoHeight){
			//var tempHeight = theDiv.parentNode.offsetHeight - (2 * Common.accordionStepHeight);
			var tempHeight = (theDiv.parentNode.offsetHeight - step);
			if(tempHeight < theDiv.parentNode._uptoHeight){
				tempHeight = theDiv.parentNode._uptoHeight;
			}
			theDiv.parentNode.style.height = tempHeight + "px";
			theDiv.parentNode._hideAccordionDivTimer = setTimeout( function(){ Common.slowHideAccordionDIV(theDiv, step, timer) }, timer );
		}
	},
	//~~FOR ACCORDIONULS
	
	//FOR promoBlockBusinessEnquiryForm
	handleEnquiryForm : function(){
		var businessEnquiryFormSubmitter = document.getElementById('businessEnquiryFormSubmitter');
		businessEnquiryFormSubmitter.disabled = true;
		var doneFocusing = false;
		var theSpan;
		
		var name = document.getElementById('name');
		theSpan = name.nextSibling;
		if(theSpan.nodeName != "SPAN"){
			theSpan = theSpan.nextSibling;
		}
		if( Common.trim(name.value) == ""){
			theSpan.style.display = "block";
			name.value = "";
			name.focus();
			doneFocusing = true;
		}
		else{
			theSpan.style.display = "none";
		}
		
		var emailaddress = document.getElementById('emailaddress');
		theSpan = emailaddress.nextSibling;
		if(theSpan.nodeName != "SPAN"){
			theSpan = theSpan.nextSibling;
		}
		if( Common.trim(emailaddress.value) == ""){
			theSpan.style.display = "block";
			emailaddress.value = "";
			if(!doneFocusing){
				emailaddress.focus();
				doneFocusing = true;
			}
		}
		else{
			theSpan.style.display = "none";
		}
		
		var subject = document.getElementById('subject');
		theSpan = subject.nextSibling;
		if(theSpan.nodeName != "SPAN"){
			theSpan = theSpan.nextSibling;
		}
		if(subject.selectedIndex == 0){
			theSpan.style.display = "block";
			if(!doneFocusing){
				subject.focus();
				doneFocusing = true;
			}
		}
		else{
			theSpan.style.display = "none";
		}
		
		var requestdetails = document.getElementById('requestdetails');
		var theSpan = requestdetails.nextSibling;
		if(theSpan.nodeName != "SPAN"){
			theSpan = theSpan.nextSibling;
		}
		if( Common.trim(requestdetails.value) == ""){
			theSpan.style.display = "block";
			requestdetails.value = "";
			if(!doneFocusing){
				requestdetails.focus();
				doneFocusing = true;
			}
		}
		else{
			theSpan.style.display = "none";
		}
		
		if(!doneFocusing){
			return true;
		}
		else{
			businessEnquiryFormSubmitter.disabled = false;
			return false;
		}
	},
	//~~FOR promoBlockBusinessEnquiryForm
	
	//FOR bulkDownloadForm
	handleBulkDownloadForm : function(){
		var atLeastOneChecked = false;
		for(var i = 0 ; i < this.brochureName.length ; i++){
			if( this.brochureName[i].checked ){
				atLeastOneChecked = true;
				break;
			}
		}
		if(atLeastOneChecked){
			Common._bulkDownloadFormErrorP.style.display = "none";
			return true;
		}
		else{
			Common._bulkDownloadFormErrorP.style.display = "block";
			return false;
		}
	}
	//~~FOR bulkDownloadForm
};

Main.start(Common);
