function showProductPersonalization(){
    var _url = getItem('_url').value;
    var _mode = getItem('_mode').value;
    var _clientid = getItem('_clientid').value;
    var _customizationid = getItem('_customizationid').value;
    var _domain = getItem('_domain').value;
    xmlHTTP('Root','proxy','_url='+_url+'&mode='+_mode+'&clientid='+_clientid+'&customizationid='+_customizationid+"&domain="+_domain,'','YourProductPersonalization');
}

function turnTemplateOn(counter){
    var template = getItem('template'+counter);
    if (template.style.display=='none'){
        if (getItem('template'+counter+'_loaded').value == 0){
            var _url = getItem('_url'+counter).value;
            xmlHTTP('Root','proxy','_url='+_url,'','template'+counter);
            getItem('template'+counter+'_loaded').value = 1;
        }
        template.style.display='';
    }
}

function turnTemplateOff(counter){
    var template = getItem('template'+counter);
    if (template.style.display=='none'){
    } else {
        template.style.display='none';
    }
}

function setValue(field, val){
    getItem(field).value=val;
}

function isValidQuantity(min, max){
    var quantity = parseInt(getTextVal('input_quantity'));
    min = parseInt(min);
    max = parseInt(max);
    if ((quantity >= min) && (quantity <= max)){
	setValue('Quantity', quantity);
	return true;
    } else {
	setValue('Quantity', '');
	alert("Quantity must be between " + min + " and " + max);
	return false;
    }
}

function checkQuantities(CartCount){
    var message = "";
    for (var x=1; x<=CartCount; x++){
	if (getTextVal('Position'+x) != ""){
	    var quantity = parseInt(getTextVal('Quantity'+x));
	    var min = parseInt(getTextVal('Min'+x));
	    var max = parseInt(getTextVal('Max'+x));
	    var setqty = getTextVal('SetQty'+x);

	    if (setqty){
		var setqtyAR=setqty.split(",");
		var validQty = false;
		for (var i=0; i<setqtyAR.length; i++){
		    if (quantity == setqtyAR[i]){
			validQty=true;
			break;
		    }
		}
		if (!validQty){
		    message += "\n Item #"+getTextVal('Position'+x)+" "+getTextVal('Product'+x)+": Quantity must be one of the following "+setqty;
		}
	    } else if (min && max){
		if ((quantity < min) || (quantity > max)){
		    message += "\n Item #"+getTextVal('Position'+x)+" "+getTextVal('Product'+x)+": Quantity must be between "+min+" - "+max;
		}
	    }
	}
    }
    if (message == ""){
	return true;
    } else {
	alert("Please fix the following: "+message);
	return false;
    }
}
function checkRegistration(){
    var message = "";
    if (getTextVal('FirstName') == "") message += "\nFirst Name Required";
    if (getTextVal('LastName') == "") message += "\nLast Name Required";
    if (getTextVal('Phone') == "") message += "\nPhone Required";
    else if (!checkInternationalPhone(getTextVal('Phone'))) message += "\nPhone Invalid";
    if (getTextVal('Email') == "") message += "\nEmail Required";
    else if (!isValidEmail(getTextVal('Email'))) message += "\nEmail Invalid";
    if (getTextVal('Confirm_Email') == "") message += "\nConfirm Email Required";
    else if (getTextVal('Email') != getTextVal('Confirm_Email')) message += "\nEmail & Confirm Email do not Match";
    if (getTextVal('Password') == "") message += "\nPassword Required";
    if (getTextVal('Password').length < 6) message += "\nPassword must be at least 6 characters.";
    if (getTextVal('Confirm_Password') == "") message += "\nConfirm Password Required";
    else if (getTextVal('Password') != getTextVal('Confirm_Password')) message += "\nPassword & Confirm Password do not Match";


    if (message == ""){
	return true;
    } else {
	alert("Please fix the following: "+message);
	return false;
    }
}

function sameAsShipping(){
    if (getItem('sameasshipping').checked){
	getItem('billingfields').style.display='none';
    } else {
	getItem('billingfields').style.display='';
    }
}

function checkShippingAndBilling(){
    var message = "";
    if (getTextVal('ShippingFirstName') == "") message += "\nShipping First Name Required";
    if (getTextVal('ShippingLastName') == "") message += "\nShipping Last Name Required";
    if (getTextVal('ShippingEmail') == "") message += "\nShipping Email Required";
    if (getTextVal('ShippingPhone') == "") message += "\nShipping Phone Required";
    else if (!checkInternationalPhone(getTextVal('ShippingPhone'))) message += "\nShipping Phone Invalid";
    else if (!isValidEmail(getTextVal('ShippingEmail'))) message += "\nEmail Invalid";
    if (getTextVal('ShippingAddress1') == "") message += "\nShipping Address Required";
    if (getTextVal('ShippingCity') == "") message += "\nShipping City Required";
    if (getTextVal('ShippingProvince') == "") message += "\nShipping Province Required";
    if (getTextVal('ShippingPostal') == "") message += "\nShipping Postal Required";
    if (getTextVal('ShippingPhone') == "") message += "\nShipping Phone Required";
    if (!getItem('sameasshipping').checked){
	if (getTextVal('BillingFirstName') == "") message += "\nBilling First Name Required";
	if (getTextVal('BillingLastName') == "") message += "\nBilling Last Name Required";
	if (getTextVal('BillingAddress1') == "") message += "\nBilling Address Required";
	if (getTextVal('BillingCity') == "") message += "\nBilling City Required";
	if (getTextVal('BillingProvince') == "") message += "\nBilling Province Required";
	if (getTextVal('BillingPostal') == "") message += "\nBilling Postal Required";
    }
    if (message == ""){
	return true;
    } else {
	alert("Please fix the following: "+message);
	return false;
    }
}

function checkPaymentInfo(){
    var message = "";
    if (getTextVal('bf') == "") message += "\nName on Card (First Name) is required";
    if (getTextVal('bl') == "") message += "\nName on Card (Last Name) is required";
    if (getTextVal('a') == "") message += "\nCard is required";
    if (getTextVal('c') == "") message += "\nCard number is required";
    else {
	if (parseInt(getTextVal('c'))){
	    getItem('c').value = parseInt(getTextVal('c'));
	} else {
	    getItem('c').value = "";
	}
	if (getTextVal('c').length != 16) message += "\nCard number must be 16 digits long";
    }
    if (getTextVal('d1') == "") message += "\nExpiration month is required";
    if (getTextVal('d2') == "") message += "\nExpiration year is required";
    if (getTextVal('e') == "") message += "\nCard verification is required";
    else {
	if (parseInt(getTextVal('e'))){
	    getItem('e').value = parseInt(getTextVal('e'));
	} else {
	    getItem('e').value = "";
	}
	if (getTextVal('e').length < 3) message += "\nCard verification must be 3-4 digits long";
    }
    if (!getItem('iagree').checked) message += "\nPlease agree to the terms and condition.";
    if (message == ""){
	return true;
    } else {
	alert("Please fix the following: "+message);
	return false;
    }
}

function checkCustomerLogin(){
    var message = "";
    if (getTextVal('email') == "") message += "\nEmail is required";
    if (getTextVal('password') == "") message += "\nPassword is required";
    if (getTextVal('password').length < 6) message += "\nPassword must be at least 6 characters.";
    if (message == ""){
	return true;
    } else {
	alert("Please fix the following: "+message);
	return false;
    }
}

function selectPaperColour(code, color, text){
    var display = "<table border=0 cellpadding=2 cellspacing=0 class=colorSelection width=130><tr><td width=45><div style=\"width:45px;height:10px;background-color:"+color+"\">&nbsp;</div></td><td width=85>"+text+"</td></tr></table>";
    getItem('papercolour_span').innerHTML=display;
    getItem('papercolour_table').style.display='none';
    getItem('ProductCode').value=code;
    getItem('ProductColour').value=color;
    getItem('ProductText').value=text;
}

function selectHighlighterColour(code, color, text){
    var display = "<table border=0 cellpadding=2 cellspacing=0 class=colorSelection width=130><tr><td width=45><div style=\"width:45px;height:10px;background-color:"+color+"\">&nbsp;</div></td><td width=85>"+text+"</td></tr></table>";
    getItem('highlightercolour_span').innerHTML=display;
    getItem('highlightercolour_table').style.display='none';
    getItem('HighlighterCode').value=code;
    getItem('HighlighterColour').value=color;
    getItem('HighlighterText').value=text;
}

function selectDieCut(id, name){
    var display = "<table border=0 cellpadding=2 cellspacing=0 class=colorSelection width=130><tr><td width=45><img width=40 height=40 src='/static/images/products/diecuts/"+id+".jpg' /></td><td width=85>"+name+"</td></tr></table>";
    getItem('diecut_span').innerHTML=display;
    getItem('diecut_table').style.display='none';
    getItem('DieCut').value=id;
    getItem('DieCutName').value=name;
}

function toggle(field, imgpath){
    var item = getItem(field);
    if (item.style.display == "none"){
	item.style.display="";
	itemImage = getItem(field+"Image");
	if (itemImage){
	    itemImage.src=imgpath+field+"Exp.gif";
	}
    } else {
	item.style.display="none";
	itemImage = getItem(field+"Image");
	if (itemImage){
	    itemImage.src=imgpath+field+".gif";
	}
    }
}

function hide(field){
    getItem(field).style.display = "none";
}
function show(field){
    getItem(field).style.display = "";
}

function initInk(num, size){

}

function selectInkColour(prefix, num, code, color, text){
    var display = "<table border=0 cellpadding=2 cellspacing=0 class=colorSelection width=130><tr><td width=45><div style=\"width:45px;height:10px;background-color:"+color+"\">&nbsp;</div></td><td width=85>"+text+"</td></tr></table>";
    getItem('ink_span_'+num).innerHTML=display;
    getItem(prefix+num).style.display='none';
    getItem('InkCode'+num).value=code;
    getItem('InkColour'+num).value=color;
    getItem('InkText'+num).value=text;
    getItem("inkcolourHTML"+num).innerHTML = "";
}


function toggleInk(prefix, num, size){

    for (i=1; i<=size; i++){
	if (i != num) getItem("inkcolourHTML"+i).innerHTML="";
    }

    if (prefix == "inkcolourHTML"){
	var thisHTML = getItem("inkcolourHTML"+num);
	if (thisHTML.innerHTML == ""){
	    var colourHTML = getItem("htmlColourHolder").innerHTML;
	    var newHTML = colourHTML.replace(/INKNUM/g, num);
	    newHTML = newHTML.replace(/INKSIZE/g, size);
	    thisHTML.innerHTML = newHTML;
	} else {
	    thisHTML.innerHTML = "";
	}

    } else if (prefix == "ink_table_"){
	getItem("inkcolourHTML"+num).innerHTML = "";

    } else if (prefix == "ink_table_all_"){
	getItem("ink_table_"+num).style.display = "none";
	var thisItem = getItem("ink_table_all_"+num);
	if (thisItem.style.display == "none") thisItem.style.display = "";
	else getItem("inkcolourHTML"+num).innerHTML = "";
    }
}



function searchBox(todo){
    var searchItem = getItem('SearchFor');
    alert(searchItem.value);
    if (todo == "in"){
        if (searchItem.value == "[Keyword or Item #]"){
            searchItem.value = "";
        }
    } else if (todo == "out"){
        if (searchItem.value == ""){
            searchItem.value = "[Keyword or Item #]";
        }
    }
}


// Paramitize encoded string for an Ajax POST request
function param (string){
    var fields=string.split(",");
    var result="";
    for (var i=0, len=fields.length; i<len; ++i){
	    var func = fields[i].split("|");
	    result += ((result=="")?'':'&') + func[0] + "=" + eval('get'+func[1]+'Val("'+func[0]+'");');
    }
    return result;
}

// Compatible with all major browsers for getting an instance of an object
function getItem(id){
    var itm = false;
    if(document.getElementById)itm = document.getElementById(id);
    else if(document.all)itm = document.all[id];
    else if(document.layers)itm = document.layers[id];
    return itm;
}

// Extract the value from a Text input box
function getTextVal (id){
    return getItem(id).value;
}

// Exrtact the value from a select list
function getSelectVal (id){
    var item = getItem(id);
    return item.options[item.selectedIndex].value;
}

// Extract a value from radio buttons
function getRadioVal (id){
    var selection = getItem(id);
    for (i=0; i<selection.length; i++)
	if (selection[i].checked == true)
	return selection[i].value;
}


function isInteger(s)
{   var i;
    for (i = 0; i < s.length; i++)
    {
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}
function trim(s)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not a whitespace, append to returnString.
    for (i = 0; i < s.length; i++)
    {
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (c != " ") returnString += c;
    }
    return returnString;
}
function stripCharsInBag(s, bag)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++)
    {
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function checkInternationalPhone(strPhone){
var bracket=3
var phoneNumberDelimiters = "()- x";
var validWorldPhoneChars = phoneNumberDelimiters + "+";
var minDigitsInIPhoneNumber = 10;
strPhone=trim(strPhone)
if(strPhone.indexOf("+")>1) return false
if(strPhone.indexOf("-")!=-1)bracket=bracket+1
if(strPhone.indexOf("(")!=-1 && strPhone.indexOf("(")>bracket)return false
var brchr=strPhone.indexOf("(")
if(strPhone.indexOf("(")!=-1 && strPhone.charAt(brchr+2)!=")")return false
if(strPhone.indexOf("(")==-1 && strPhone.indexOf(")")!=-1)return false

s=stripCharsInBag(strPhone,validWorldPhoneChars);
return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}



function isValidEmail(str) {

    var at="@"
    var dot="."
    var lat=str.indexOf(at)
    var lstr=str.length
    var ldot=str.indexOf(dot)

    if (str.indexOf(at)==-1){
       return false
    }

    if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
       return false
    }

    if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
	return false
    }

     if (str.indexOf(at,(lat+1))!=-1){
	return false
     }

     if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
	return false
     }

     if (str.indexOf(dot,(lat+2))==-1){
	return false
     }

     if (str.indexOf(" ")!=-1){
	return false
     }

     return true
}
function openw(URL){
    new_window = window.open(URL, "Image", "height=0,width=0,toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=no")
}

function popup(url,w,h,s,r){
	if(!w)w=700;if(!h)h=500;if(s==null)s="yes";if(r==null)r="yes";
	window.open(url,"Popup", "height="+h+",width="+w+",toolbar=no,location=no,status=no,menubar=no,scrollbars="+s+",resizable="+r);
}
//
function MM_preloadImages() {
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}
//
//if (document.images){
//   img1 = new Image();img2 = new Image();img3 = new Image();img4 = new Image();img5 = new Image();img6 = new Image();
//   img1.src = "/static/images/srw/Corporate/frame/nav/mo/notes.gif";
//   img2.src = "/static/images/srw/Corporate/frame/nav/mo/die_cut.gif";
//   img3.src = "/static/images/srw/Corporate/frame/nav/mo/cubes.gif";
//   img4.src = "/static/images/srw/Corporate/frame/nav/mo/dispensers.gif";
//   img5.src = "/static/images/srw/Corporate/frame/nav/mo/gift_sets.gif";
//   img6.src = "/static/images/srw/Corporate/frame/nav/mo/flags.gif";
//}
//
//
MM_preloadImages('/static/images/srw/corporate/frame/nav/mo/notes.gif',
				 '/static/images/srw/corporate/frame/nav/mo/die_cut.gif',
				 '/static/images/srw/corporate/frame/nav/mo/cubes.gif',
				 '/static/images/srw/corporate/frame/nav/mo/dispensers.gif',
				 '/static/images/srw/corporate/frame/nav/mo/gift_sets.gif',
				 '/static/images/srw/corporate/frame/nav/mo/flags.gif'
				 );
