// SCRIPTS FOR THE EWS PROJECT

function trim(str, chars) {
    return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

function hide(id) {
    var content = document.getElementById(id);
    if (content) { content.style.display = "none"; }
}

function show(id)
{
    var content = document.getElementById(id);
    if (content && content.style.display == "none") { content.style.display = ""; }
}

// SHOWS AN ELEMENT IF HIDDEN
function expand(id)
{
	show(id)
}


// SHOWS OR HIDES AN ELEMENT
function showHide(id) {
	if (document.getElementById(id).style.display == "none") {
		document.getElementById(id).style.display = "";
	} else {
		document.getElementById(id).style.display = "none";
	}
}

// GET VALUE
function getElementValue(id) {
	var content = document.getElementById(id);
	if (!content) { 
		return ""; 
	}
	return content.value;
}

// FUNCTION FOR DISPLAYING TOOLTIP POPUPS
function tooltip(e, element, width) {

	var element = document.getElementById(element);

	if (element.style.visibility == 'hidden') {
		var posLeft;
		var posTop;
		if (document.all) {
			posLeft = event.clientX + document.documentElement.scrollLeft;
			posTop = event.clientY + document.documentElement.scrollTop;
		} else {
			posLeft = e.pageX;
			posTop = e.pageY;
		}
		posLeft = posLeft + 15;
		posTop = posTop + 15;
		element.style.left = posLeft + 'px';
		element.style.top = posTop + 'px';
		if (width == undefined) {
			var width = 250;
		}
		element.style.width = width + 'px';
		element.style.visibility = 'visible';

	} else {
		element.style.visibility = 'hidden';
	}
}


// IMAGE SWAP FUNCTION FOR MAIN PRODUCT IMAGE
function swapProductImage(imageSRC) {
	if (document.getElementById('productImg')) {
		document.getElementById('productImg').setAttribute('src', imageSRC);
	}
}

// Function for swapping images for colour
function swapImage(ele1,ele2, srcUrl) {

	var main_img = 'productImg_'+ele1;
	changeImage(main_img, srcUrl);

  	if(ele1 == ele2)
   		return;

  	var x = document.getElementsByTagName('div');      
  	for (var i=0;i<x.length;i++)
  	{
   		var div_id = x[i].id+"";
  		// alert(div_id.indexOf(ele2));
   		if(div_id.indexOf(ele2)>0) {
    			x[i].style.display = 'inline';
   		}

   		if(div_id.indexOf(ele1)>0) {
    			x[i].style.display = 'none';
   		}
	}
}


// TAB SWAPPING FUNCTION FOR PRODUCT POPUPS
function swapTab(tabID) {
	switch(tabID) {
		case 'popupOverview':
			document.getElementById('popupOverview').style.display = 'block';
			document.getElementById('popupSpecifications').style.display = 'none';
			document.getElementById('popupWhatsInBox').style.display = 'none';
		break
					
		case 'popupSpecifications':
			document.getElementById('popupOverview').style.display = 'none';
			document.getElementById('popupSpecifications').style.display = 'block';
			document.getElementById('popupWhatsInBox').style.display = 'none';
		break
					
		case 'popupWhatsInBox':
			document.getElementById('popupOverview').style.display = 'none';
			document.getElementById('popupSpecifications').style.display = 'none';
			document.getElementById('popupWhatsInBox').style.display = 'block';
		break
		
		default:
			document.getElementById('popupOverview').style.display = 'block';
			document.getElementById('popupSpecifications').style.display = 'none';
			document.getElementById('popupWhatsInBox').style.display = 'none';
		}
}

// NEW POPUP WINDOW FUNCTION
function popUp ( URL, thewidth, theheight, theleft, thetop, thetoolbar, thescrollbars, thelocationbar, thestatus, themenubar, theresizable, id ) {
	if ( URL == undefined ){
		URL = 'http://www.carphonewarehouse.com';
	}
	
	if ( id == undefined ){ id = 'cpw' }
	if ( thetoolbar == undefined ) { thetoolbar = 0 }
	if ( thescrollbars == undefined ) { thescrollbars = 1 }
	if ( thelocationbar == undefined ) { thelocationbar = 0 }
	if ( thestatus == undefined ) { thestatus = 0 }
	if ( themenubar == undefined ) { themenubar = 0 }
	if ( theresizable == undefined ) { theresizable = 0 }
	if ( thewidth == undefined ) { thewidth = 800 }
	if ( theheight == undefined ) { theheight = 400 }
	if ( theleft == undefined ) { theleft = 200 }
	if ( thetop == undefined ) { thetop = 100 }
	
	eval( "var cpwPopup = window.open('"+URL+"','"+id+"','toolbar="+thetoolbar+",scrollbars="+thescrollbars+",location="+thelocationbar+",status="+thestatus+",menubar="+themenubar+",resizable="+theresizable+",width="+thewidth+",height="+theheight+",left = "+theleft+",top = "+thetop+"');");
	if ( cpwPopup ) {
		return false;
	} else {
		return true;
	}
}

// FUNCTION FOR CHANGING IMAGES FOR CHOOSE VIEW
function changeImage( imgElementId, imgPath ) {
	document.getElementById(imgElementId).src = imgPath; 
}

//Function to remove spaces from form fields
function removeSpacesInFormField(fieldName) {
	var object = document.getElementById(fieldName);
	if (!object) {
		return;
	}
 	var string = object.value;
	var tstring = "";
	string = '' + string;
	splitstring = string.split(" ");
	for(i = 0; i < splitstring.length; i++) {
		tstring += splitstring[i];
	}
	object.value = tstring;
}

// SHOW HIDE FUNCTION
function newShowHide(elementId, button) {
    if (document.getElementById(elementId)) {
        if (document.getElementById(elementId).style.display == 'none') {
            document.getElementById(elementId).style.display = 'block';
            if (button.src) { button.src='http://media.phonehouse.com/cpw-sales/static/images/common/buttons/btn_hide.gif'; }
        } else {
            document.getElementById(elementId).style.display = 'none';
            if (button.src) { button.src='http://media.phonehouse.com/cpw-sales/static/images/common/buttons/btn_show.gif'; }
        }
    }
}

