// CLEAR AND SHOW FORM DATA //

var yyyyy = 0;

var errstringVal = '';

function concatCollections() {
	var c, k, j, i = arguments.length;
	var a = [];
	for ( j=0; j<i; j++ ) {
		c = arguments[j];
		h = c.length;
		for ( k=0; k<h; k++ ){
			a.push(c[k]);
		}
	}
	return a;
}

function addEvent(element, eventType, lamdaFunction, useCapture) {
    if (element.addEventListener) {
        element.addEventListener(eventType, lamdaFunction, useCapture);
        return true;
    } else if (element.attachEvent) {
        var r = element.attachEvent('on' + eventType, lamdaFunction);
        return r;
    } else {
        return false;
    }
}

function knackerEvent(eventObject) {
    if (eventObject && eventObject.stopPropagation) {
        eventObject.stopPropagation();
    }
    if (window.event && window.event.cancelBubble ) {
        window.event.cancelBubble = true;
    }
    
    if (eventObject && eventObject.preventDefault) {
        eventObject.preventDefault();
    }
    if (window.event) {
        window.event.returnValue = false;
    }
}

function cancelEventSafari() {
    return false;        
}

function getElementStyle(elementID, CssStyleProperty) {
    var element = document.getElementById(elementID);
    if (element.currentStyle) {
        return element.currentStyle[toCamelCase(CssStyleProperty)];
    } else if (window.getComputedStyle) {
        var compStyle = window.getComputedStyle(element, '');
        return compStyle.getPropertyValue(CssStyleProperty);
    } else {
        return '';
    }
}

function toCamelCase(CssProperty) {
    var stringArray = CssProperty.toLowerCase().split('-');
    if (stringArray.length == 1) {
        return stringArray[0];
    }
    var ret = (CssProperty.indexOf("-") == 0)
              ? stringArray[0].charAt(0).toUpperCase() + stringArray[0].substring(1)
              : stringArray[0];
    for (var i = 1; i < stringArray.length; i++) {
        var s = stringArray[i];
        ret += s.charAt(0).toUpperCase() + s.substring(1);
    }
    return ret;
}

function disableTestLinks() {
  var pageLinks = document.getElementsByTagName('a');
  for (var i=0; i<pageLinks.length; i++) {
    if (pageLinks[i].href.match(/[^#]#$/)) {
      addEvent(pageLinks[i], 'click', knackerEvent, false);
    }
  }
}

function createCookie(name, value, days) {
    var expires = '';
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days*24*60*60*1000));
        var expires = '; expires=' + date.toGMTString();
    }
    document.cookie = name + '=' + value + expires + '; path=/';
}

function readCookie(name) {
    var cookieCrumbs = document.cookie.split(';');
    var nameToFind = name + '=';
    for (var i = 0; i < cookieCrumbs.length; i++) {
        var crumb = cookieCrumbs[i];
        while (crumb.charAt(0) == ' ') {
            crumb = crumb.substring(1, crumb.length);
        }
        if (crumb.indexOf(nameToFind) == 0) {
            return crumb.substring(nameToFind.length, crumb.length);
        }
    }
    return null;
}

function eraseCookie(name) {
    createCookie(name, '', -1);
}




addEvent(window, 'load', init, false);

function init() {
    var formInputs1 = document.getElementsByTagName('input');
	var formInputs2 = document.getElementsByTagName('textarea');
	var formInputs = concatCollections(formInputs1, formInputs2);
	var inputId;
	//    for (var i = 0; i < formInputs.length; i++) {
    for (var i = 0; i < formInputs.length; i++) {
        var theInput = formInputs[i];

        if (theInput.type == 'text' || theInput.className == 'text' || theInput.type == 'password') {
            //SER adding search filter to summaryhistory page
            //since the textboxes not getting cleared, id check with 'txtPlain' added. so if textbox id starts with txtPlain, will act like normal textbox.
            inputId = theInput.id.toLowerCase();
            if (theInput.type == 'text' && inputId.indexOf('txtplain') != -1)
            { }
            else {
                addEvent(theInput, 'focus', clearDefaultText, false);
                addEvent(theInput, 'blur', replaceDefaultText, false);

                if (theInput.value != '') {
                    theInput.defaultText = theInput.value;
                }
            }
        }
    }
}

function clearDefaultText(e) 
{
    var target = window.event ? window.event.srcElement : e ? e.target : null;
    if (!target) return;
    
    if (target.value == target.defaultText) 
    {
        if(target.defaultTex == 'Enter Product Codea')
        {
        var y = 5;
        }
        else
        {

        target.value = '';
        }
    }
}

function replaceDefaultText(e) {
    var target = window.event ? window.event.srcElement : e ? e.target : null;
    if (!target) return;
    
    if (target.value == '' && target.defaultText) {
        if(target.defaultTex == 'Enter Product Codea')
        {
        var y = 5;
        }
        else
        {
        target.value = target.defaultText;
        }
    }
}

addEvent(window, 'load', clear, false);

function clear() {
	if (document.getElementById('clear')){
		document.getElementById('clear').onclick=function(){
			if (document.getElementById('username')){
				document.getElementById('username').value='Username';
			}
			if (document.getElementById('passwordDummie')){
				document.getElementById('passwordDummie').value='Password'; 
				document.getElementById('passwordDummie').style.display = '';
			}
			if (document.getElementById('passwordReal')){
				document.getElementById('passwordReal').style.display = 'none';
				document.getElementById('passwordReal').value=''; 
			}
			if (document.getElementById('forgotten')){
				document.getElementById('forgotten').value='Enter email address'; 
			}
			return false;
		}
	}
}

addEvent(window, 'load', password, false);

function password() {
	if (document.getElementById('passwordDummie') && document.getElementById('passwordReal')){
		document.getElementById('passwordDummie').onfocus=function(){
			this.style.display = 'none';
			document.getElementById('passwordReal').style.display = '';
			document.getElementById('passwordReal').focus();
		}
		document.getElementById('passwordReal').onblur=function(){
			if(this.value == ''){
				this.style.display = 'none';
				document.getElementById('passwordDummie').style.display = '';
			}
		}
	}
}


// TABLE ROW HOVER //

addEvent(window, 'load', rows, false);

function rows() {
	if (document.getElementById('scroller')){
		var tableRows = document.getElementById('scroller').getElementsByTagName('tr');
		for (var i = 0; i < tableRows.length; i++) {
			tableRows[i].onmouseover=function(){
				this.style.background = '#E8E8E8';
			}
			tableRows[i].onmouseout=function(){
				this.style.background = 'none';
			}
		}
	}
	if (document.getElementById('wideTable'))
	{
		var tableRows = document.getElementById('wideTable').getElementsByTagName('tr');
		for (var i = 0; i < tableRows.length; i++) 
		{
			tableRows[i].onmouseover=function()
			{
				var myRegExp = /associatedProducts/;
				var string1 = "'" + this.className + "'";
				var matchPos1 = string1.search(myRegExp);
				if(matchPos1 == -1)
				{
				
				    try
				    {
				            stidd = this.id;
				            if (stidd.indexOf('_accessory') > 1)
				            {
				                this.style.background = '#A0D5DA';
				            }
				            else
				            {
					            this.style.background = '#E8E8E8';
					        }
				    }
				    catch(err)
				    {
    					    					
					    this.style.background = '#E8E8E8';
				    }
				    
				    this.style.background = '#E8E8E8';

				}
				else
				{
				    this.style.background = '#A0D5DA';
				}
				
			}
			tableRows[i].onmouseout=function()
			{
				var myRegExp = /associatedProducts/;
				var string1 = "'" + this.className + "'";
				var matchPos1 = string1.search(myRegExp);
				if(matchPos1 == -1)
                {
                    try
                    {
                	    var stidd = '';
				        stidd = this.id;
    				    
				        if (stidd.indexOf('_accessory') > 1)
				        {
				            this.style.background = '#CAD7DB';
				        }
				        else
				        {
					        this.style.background = 'none';
					    }
					}
                    catch(err)
                    {                
   					    this.style.background = 'none';
   					}
					
				}
			}
		}
	}
}


// FILTER QUESTIONS RESIZE //

//addEvent(window, 'load', setSize, false);

function setSize()
{
	if (document.getElementById('schematic') && document.getElementById('scroller')){
		document.getElementById('scroller').style.height = (document.getElementById('schematic').offsetHeight - 46) + 'px';
	}
}


// HIDE AND SHOW ASSOCIATED PRODUCTS //

function collectElementbyClass(classname){
    var classnameRE=new RegExp("(^|\\s+)"+classname+"($|\\s+)", "i")
    var pieces=[]
    var alltags=document.all? document.all : document.getElementsByTagName("*")
    for (var i=0; i<alltags.length; i++){
        if (typeof alltags[i].className=="string" && alltags[i].className.search(classnameRE)!=-1)
            pieces[pieces.length]=alltags[i]
    }
    return pieces
}

addEvent(window, 'load', associatedProducts, false);

var prodCode = "";

function associatedProducts(){
	var targetBtn=this.collectElementbyClass("tableBtn");
	for (var i=0; i<targetBtn.length; i++){
		if (targetBtn[i].getAttribute("rel")){
			targetBtn[i].onclick=function(){
				prodCode = this.rel
				openProducts();
				return false;
			}
		}
	}
	var targetHide=this.collectElementbyClass("hideButton");
	for (var i=0; i<targetHide.length; i++){
		targetHide[i].onclick=function(){
			prodCode = this.rel
			closeProducts();
			return false;
		}
	}
}

function openProducts(){
	var targetProduct=this.collectElementbyClass(prodCode);
	for (var i=0; i<targetProduct.length; i++){
		if(targetProduct[i].style.display == "none"){
			targetProduct[i].style.display = "";
		}
		else{
			targetProduct[i].style.display = "none";
		}
	}
}

function closeProducts(){
	var targetProduct=this.collectElementbyClass(prodCode);
	for (var i=0; i<targetProduct.length; i++){
		targetProduct[i].style.display = "none";
	}
}


// FAQ HIDE AND SHOW //

addEvent(window, 'load', faq, false);

function faq() {
	if (document.getElementById('faq')){
		var faqRows = document.getElementById('faq').getElementsByTagName('li');
		for (var i = 0; i < faqRows.length; i++) {
			faqRows[i].onclick=function(){
				var theDiv = this.getElementsByTagName('div');
				if(theDiv[0].style.display == 'block'){
					theDiv[0].style.display = 'none';
					this.style.paddingBottom = '8px';
				}
				else{
					theDiv[0].style.display = 'block';
					this.style.paddingBottom = '0';
				}
			}
			faqRows[i].onselectstart = function(){
				return false;
			}
			faqRows[i].onmousedown = function(){
				return false;
			}
		}
	}
}


// COLLECT OR DELIVER SHOW AND HIDE ///

addEvent(window, 'load', collDel, false);

function collDel(){
	if (document.getElementById('collect') && document.getElementById('deliver')){
		document.getElementById('collect').onclick = function(){
			document.getElementById('deliverAddress').style.display = 'none';
		}
		document.getElementById('deliver').onclick = function(){
			document.getElementById('deliverAddress').style.display = '';
		}
	}
}

if(errstringVal != '') alert(errstringVal);
