// Define trim function
function stringTrim() {
  return this.replace(/^\s+/, '').replace(/\s+$/, '');
}
String.prototype.trim = stringTrim;

// Check email address
function validEmail(email) {
    var emailRegex = /^(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])+$/i;
    if (email.match(emailRegex)) {
        return true;
    }
    return false;
}

// Validate zip code
function validZipCode(input) {
  // TODO: consider other countries
  validChars = "0123456789-";
  myLength = input.length;
  if (myLength != 5 && myLength != 9 && myLength != 10) {
    return false;
  }
  for (i=0; i<myLength; i++) {
    positionChar = input.charAt(i);
    if (validChars.indexOf(positionChar,0) < 0) {
       return false;
    }
  }
  return true;
}

// Validate phone number
function validPhone(input) {
  // TODO: consider other countries
  validChars = "0123456789- ext.()";
  myLength = input.length;
  if (myLength < 10) {
    return false;
  }  
  for (i=0; i<myLength; i++) {
    positionChar = input.charAt(i);
    if (validChars.indexOf(positionChar,0) < 0) {
       return false;
    }
  }
  return true;
}

// Smart search
function smartSearch(domain) {
  form = document.form1;
  var keyword = form.txtSearch.value;
  //keyword = keyword.replace(/\W/g, " ");
  //keyword = keyword.replace(/ +/g, " ");   
  keyword = keyword.trim();
  if(keyword != "") {
    document.location.href = domain + "search.aspx?keyword=" + escape(keyword);
  }
}

// Check keyword
function checkKeyword(domain,vthis,vevent) {
  form = document.form1;  
  if(vevent.keyCode == 13) {
    //smartSearch;
    var keyword = vthis.value;
    keyword = keyword.trim();
    if(keyword != "") {
      document.location.href = domain + "search.aspx?keyword=" + escape(keyword);
    }
    return false;
  } else {
    return true;
  }
}

// Convert invalid characters
function specialCharsConverter(inString) {
  var outString = inString;
  var charMidDot = String.fromCharCode(183);
  outString = outString.replace(/’/g, "'")
  outString = outString.replace(/–/g, "-")
  outString = outString.replace(/—/g, "-")
  outString = outString.replace(/•/g, charMidDot)
  return outString 
}

// Live chat - Old version keeps for compatibility
function openChat() {
  var theURL = "https://server.iad.liveperson.net/hc/53452500/?cmd=file&amp;file=visitorWantsToChat&amp;site=53452500&amp;referrer=" + escape(document.location);
  window.open(theURL, "Chat53452500", "width=472px,height=320px");
}

// Live chat - partner specific 
function openChatDefault() {
  var theURL = "https://server.iad.liveperson.net/hc/53452500/?cmd=file&amp;file=visitorWantsToChat&amp;site=53452500&SESSIONVAR!skill=123PrintChat&amp;referrer=http://www.123print.com/"
  window.open(theURL, "Chat53452500", "width=472px,height=320px");
}

function openChatBloomnet() {
  var theURL = "https://server.iad.liveperson.net/hc/53452500/?cmd=file&amp;file=visitorWantsToChat&amp;site=53452500&SESSIONVAR!skill=BloomNetChat&amp;referrer=http://bloomnet.123print.com/"
  window.open(theURL, "Chat53452500", "width=472px,height=320px");
}

// Message box
function msgOKCancel(message) {
  OKOrCancel = confirm(message); 
  if (!OKOrCancel) {
    return false;
  } else {
    return true;
  }
}

//Open a intro guide window
function openWindow(url) {	
	window.open(url, '', 'width=600px,height=475px,resizable,scrollbars');
}

//To be compatible with the old version
function referViaEmail(ReferralSource, DesignID, ItemID) {
	window.open('ReferByEmail.aspx?ReferralSource=' + ReferralSource + '&DesignID=' + DesignID + '&ItemID=' + ItemID, '', 'width=600px,height=495px,resizable,scrollbars');
}

function referViaEmailForHome(ReferralSource) {
	window.open('ReferByEmail.aspx?ReferralSource=' + ReferralSource, '', 'width=600px,height=495px,resizable,scrollbars');
}

function referViaEmailForDesign(ReferralSource, DesignID, ItemID, ImgURL) {
	window.open('ReferByEmail.aspx?ReferralSource=' + ReferralSource + '&DesignID=' + DesignID + '&ItemID=' + ItemID + '&ImgURL=' + ImgURL, '', 'width=600px,height=495px,resizable,scrollbars');
}

function referViaEmailForDesignClass(ReferralSource, DesignClassID, Page, ItemID) {
	window.open('ReferByEmail.aspx?ReferralSource=' + ReferralSource + '&DesignClassID=' + DesignClassID + '&Page=' + Page + '&ItemID=' + ItemID, '', 'width=600px,height=495px,resizable,scrollbars');
}

function mouse_over_add_icon(DesignID) {
	var button_name = 'refer_button_' + DesignID;
	document.getElementById(button_name).src='images/icnEmailHover.gif ';	
}

function mouse_out_add_icon(DesignID) {
	var button_name = 'refer_button_' + DesignID;	
	document.getElementById(button_name).src='images/icnEmail.gif ';	
}

function openStudioAjaxLite(ItemID, DesignID, DocID, ucMatchingItemIdx, IdxType) {
	width = 800 + 'px';
	switch (ItemID)	{
		//Letterhead
		case 100021:			{height=830 + 'px'; break}
		//Addr Label
		case 100020:			{height=400 + 'px'; break}
		//Mailing Label
		case 100027:			{height=450 + 'px'; break}
		//#10 Envelope
		case 100018:
		case 100187:			{height=500 + 'px'; break}
		//Business Card
		case 100001:
		case 100110:			{height=600 + 'px'; break}
		//Postcard
		case 100010:			{height=600 + 'px'; break}
		//Large Memopad
		case 100025:			{height=600 + 'px'; break}
		default:				{height=600 + 'px'; break}
	}
	var leftPos = (window.screen.width-width)/2 + 'px' , topPos = (window.screen.height-height)/2 + 'px';	
	
	window.open('StudioAjaxLite.aspx?ucMatchingItemIdx=' + ucMatchingItemIdx + '&IdxType=' + IdxType + '&DocID=' + DocID + '&DesignID=' + DesignID + '&ItemID=' + ItemID, '', 'width=' + width +'px,height=' + height + 'px,top=' + topPos + 'px ,left=' + leftPos +'px,resizable,scrollbars');	
}

function getCookie(strCookieName) {
    if (document.cookie.length>0)  {
        intStart=document.cookie.indexOf(strCookieName + "=")
        if (intStart!=-1) { 
            intStart=intStart + strCookieName.length+1 
            intEnd=document.cookie.indexOf(";",intStart)
            if (intEnd==-1) intEnd=document.cookie.length
            return unescape(document.cookie.substring(intStart,intEnd))
        } 
     }
    return ""
}

function return2BCC() {
    intCartID=getCookie('cCustomerCart')
    intCartID4BCC=getCookie('cCustomerCart4BCC')
    if ((intCartID==null || intCartID=="") && (intCartID4BCC==null || intCartID4BCC=="")) {
        alert('You need to design and approve your business card before returning to the ExchangeBusisnessCards.com')
    }
    else {
        window.location = "Return2BCC.aspx"        
    }  
}

function dc_settabactive(curtab, numtab) {        
    for (i=0; i<=numtab; i++){       
        if (i != curtab){        
            document.getElementById('dc_tab' + i).className="dc_tabinactive custom";
            document.getElementById('dc_tablink' + i).className="dc_linkinactive";            
            document.getElementById('dc_tabdetail' + i).style.display = "none";
        }
    }    
    document.getElementById('dc_tab' + curtab).className="dc_tabactive custom";   
    document.getElementById('dc_tablink' + curtab).className="dc_linkactive";    
    document.getElementById('dc_tabdetail' + curtab).style.display = "block";
}

function di_settabactive(curtab, numtab) {        
    for (i=0; i<=numtab; i++){       
        if (i != curtab){        
            document.getElementById('di_tab' + i).className="di_tabinactive custom";
            document.getElementById('di_tablink' + i).className="di_linkinactive";            
            document.getElementById('di_tabdetail' + i).style.display = "none";
        }
    }    
    document.getElementById('di_tab' + curtab).className="di_tabactive custom";   
    document.getElementById('di_tablink' + curtab).className="di_linkactive";    
    document.getElementById('di_tabdetail' + curtab).style.display = "block";
}

function setTab(tab) {
	var tablist = document.getElementById('menucontents').getElementsByTagName("ul")[0].getElementsByTagName("li");
	var contentlist = document.getElementById('innercontents').getElementsByTagName("div");
	for (var i=0, il=tablist.length; i<il; i++)	{
		if (i == tab){
			tablist[i].className="selected";
			contentlist[i].style.display = 'block';
		}else{
			tablist[i].className="";
			contentlist[i].style.display = 'none';
		}
	}
}

function setTab960(tab) {
	var tablist = document.getElementById('menucontents960').getElementsByTagName("ul")[0].getElementsByTagName("li");
	var contentlist = document.getElementById('innercontents960').getElementsByTagName("div");
	for (var i=0, il=tablist.length; i<il; i++)	{
		if (i == tab){
			tablist[i].className="selected";
			contentlist[i].style.display = 'block';
		}else{
			tablist[i].className="";
			contentlist[i].style.display = 'none';
		}
	}
}

function setCagTab(tab){
		var tablist = document.getElementById('menucontents2').getElementsByTagName("ul")[0].getElementsByTagName("li");
		var contentlist = document.getElementById('innercontents2').getElementsByTagName("div");
		for (var i=0, il=tablist.length; i<il; i++){
			if (i == tab){
				tablist[i].className="selected";
				contentlist[i].style.display = 'block';
			}else{
				tablist[i].className="";
				contentlist[i].style.display = 'none';
			}
		}
	}

//Open a learn-how window
function openPriceQuotePopup(CategoryID) {	
    var windowProp = 'width=990,height=600,menubar=no,location=no,status=no,resizable=no,scrollbars=yes';
    if (CategoryID == 0)
        window.open('/PriceQuotePopUp.aspx', '', windowProp);
    else
        window.open('/PriceQuotePopUp.aspx?CategoryID=' + CategoryID, '', windowProp);
}

//Added by Zhonghuaqiang on 05/27/2008 : For Ajax automatic suggestions
/**
 * Provides suggestions for Ajax search.
 */
var ajaxsuggestion_j=-1;
var ajaxsuggestiontemp_str;
var $=function(node){
	return document.getElementById(node);
}
var $$=function(node){
	return document.getElementsByTagName(node);
}
function ajax_keyword(){
	var ajaxsuggestionXmlHttp;
	try{
		ajaxsuggestionXmlHttp=new XMLHttpRequest();
		}
	catch(e){
		try{
			ajaxsuggestionXmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
		catch(ee){
			ajaxsuggestionXmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
		
		}
	ajaxsuggestionXmlHttp.onreadystatechange=function(){
	if (ajaxsuggestionXmlHttp.readyState==4){
		if (ajaxsuggestionXmlHttp.status==200){

			var data=ajaxsuggestionXmlHttp.responseText;
	
			$("Ajaxsuggest").innerHTML=data;
			//$("suggest").style.display="block"
			ajaxsuggestion_j=-1;

			}
		}
	}
	ajaxsuggestionXmlHttp.open("post", "AutoSuggest.aspx", true);
	ajaxsuggestionXmlHttp.setRequestHeader('Content-type','application/x-www-form-urlencoded');
	ajaxsuggestionXmlHttp.send("keyword="+escape($("AjaxSuggestionKeyword").value));
}
function ajaxsuggestionkeyupdeal(e){
	var keyc;
	if(window.event){
		keyc=e.keyCode;
		}
	else if(e.which){
		keyc=e.which;
		}
	if(keyc!=40 && keyc!=38){
	    ajaxsuggestiontemp_str=$("AjaxSuggestionKeyword").value;
	    if(ajaxsuggestiontemp_str.length >= 3){
	       ajax_keyword();
	    }		
		//ajaxsuggestiontemp_str=$("keyword").value;
	}
}
function ajaxsuggestionset_style(num){
	for(var i=0;i<$$("li").length;i++){
		var li_node=$$("li")[i];
		li_node.className="AjaxSuggestionLI";
	}
	if(ajaxsuggestion_j>=0 && ajaxsuggestion_j<$$("li").length){
		var i_node=$$("li")[ajaxsuggestion_j];
		$$("li")[ajaxsuggestion_j].className="AjaxSuggestionSelect";
		}
}
function ajaxsuggestion_mo(nodevalue){
	ajaxsuggestion_j=nodevalue;
	ajaxsuggestionset_style(ajaxsuggestion_j);
}
function ajaxsuggestionform_submit(){
	if(ajaxsuggestion_j>=0 && ajaxsuggestion_j<$$("li").length){
		$$("input")[0].value=$$("li")[ajaxsuggestion_j].childNodes[0].nodeValue;
		}
	document.search.submit();
}
function ajaxsuggestionhide_suggest(){		
	$("Ajaxsuggest").innerHTML=""				
	//var nodes=document.body.childNodes
	//for(var i=0;i<nodes.length;i++){
	//	if(nodes[i]!=$("keyword")){
	//		$("Ajaxsuggest").innerHTML="";
	//		}
	//	}
}
function ajaxsuggestionkeydowndeal(e){
	var keyc;
	if(window.event){
		keyc=e.keyCode;
		}
	else if(e.which){
		keyc=e.which;
		}
	if(keyc==40 || keyc==38){
	if(keyc==40){
		if(ajaxsuggestion_j<$$("li").length){
			ajaxsuggestion_j++;
			if(ajaxsuggestion_j>=$$("li").length){
				ajaxsuggestion_j=-1;
			}
		}
		if(ajaxsuggestion_j>=$$("li").length){
				ajaxsuggestion_j=-1;
			}
	}
	if(keyc==38){
		if(ajaxsuggestion_j>=0){
			ajaxsuggestion_j--;
			if(ajaxsuggestion_j<=-1){
				ajaxsuggestion_j=$$("li").length;
			}
		}
		else{
			ajaxsuggestion_j=$$("li").length-1;
		}
	}
	ajaxsuggestionset_style(ajaxsuggestion_j);
	if(ajaxsuggestion_j>=0 && ajaxsuggestion_j<$$("li").length){
		$("AjaxSuggestionKeyword").value=$$("li")[ajaxsuggestion_j].childNodes[0].nodeValue;
		}
	else{
		$("AjaxSuggestionKeyword").value=ajaxsuggestiontemp_str;
		}
	}
}

// Set Email Watermark
function emailWatermark(sField, state) {

    var emailField = document.getElementById(sField);
    var emailValue = emailField.value;
    var defaultval = "Enter your email..."

    // Evaluate
    if (state == 'onload') {
        if (emailValue == "") {
            emailField.value = defaultval;
        }
    }
    else if (state == 'onclick'){
        if (emailValue == defaultval) {
            emailField.value = "";
        }
    }
    else if (state == 'onblur') {
        if (emailValue == "") {
            emailField.value = defaultval;
        }
        else if (emailValue == defaultval) {
            emailField.value = "";
        }
    }
}

