// JavaScript Document
function onLoad_JS_left_nav()
{
	goSetHeight('ifrm');	//automatic set iframe's height, reside at the left_menu.html
}

function onLoad_JS_template()	//for anchor & left nav. menu
{
	tinyScrolling.init();
	//run_highlight_current_page();
	//ckSize();
}
function onLoad_JS_products()
{
	tinyScrolling.init();
	//ckSize();
}
function onLoad_all_quote_pages()
{
	reset_onReload('Forminfo');	//from pcusa_overall
	reset_hidden_field();
	display_summary();
	disableFields();
	calcprintprice();
	tinyScrolling.init();
	showpic();	
	//popPromo();
	//run_highlight_current_page();
	//ckSize();
}
function onLoad_custom_quote_pages()
{
	reset_onReload('Forminfo');	//from pcusa_overall
	display_summary();
	reset_hidden_field();
	disableFields();
	tinyScrolling.init();
	//run_highlight_current_page();
	//createMenu('product_menu', 300);
	//ckSize();
}
function popupwin(url, uname, uw, uh) 
{
    window.open(url,uname,'"menubar=no,width='+uw+',height='+uh+',toolbar=no,scrollbars=1"');
}
function ckSize()
{
	if(document.getElementById("site-info"))
	{
		if(document.getElementById("main-site").offsetHeight)
		{
			var mainDivHeight = document.getElementById("main-site").offsetHeight;
			document.getElementById("site-info").style.top = mainDivHeight+'px';
		}
   		else
   		{
			document.getElementById("site-info").style.top = 1500+'px';
   		}
			document.getElementById("site-info").style.position = 'absolute';
			document.getElementById("site-info").style.left = '0px';
			document.getElementById("site-info").style.width = '100%';
	}
}
function getQueryVariable(variable) 
{
  var query = window.location.search.substring(1);
  var vars = query.split("&");
  for (var i=0;i<vars.length;i++) 
  {
    var pair = vars[i].split("=");
    if (pair[0] == variable) 
	{
      return pair[1];
    }
  } 
}
//javascript xmlhttp request
function getXmlHttpObject()
{
  var xmlHttpObject=null;
  try	// Firefox, Opera 8.0+, Safari
    {
			xmlHttpObject=new XMLHttpRequest();
    }
  catch (e)
    {
    try	// Internet Explorer
      {
			xmlHttpObject=new ActiveXObject("Msxml2.XMLHTTP");
      }
    catch (e)
      {
			//xmlHttpObject=new ActiveXObject("Microsoft.XMLHTTP");
   		try
       	{
       		xmlHttpObject=new ActiveXObject("Microsoft.XMLHTTP");
       	}
   		catch (e)
       	{
       		//do nothing
       	}
      }
    }
  return xmlHttpObject;
}
function show_image(image_folder,image_name,image_type,img_id)
{
		var pic_path = 'http://www.print-designing-studio.com/images/'+image_folder+'/';
		var pic_file_name = image_name+'.'+image_type;
		if(document[img_id].src != null)
		{
			document[img_id].src = pic_path + pic_file_name;
		}
}
function init_text(theField, original_value, display_value)	//fill text field with initial text
{
	if(theField.value == original_value || theField.value == '')
	{
		theField.value = display_value;
	}
}
//a default value will disappear onfocus, but fill it back if blur provided if the field is empty
//need an value for the txt field - as a defaultValue
//how to call: onfocus="txt_focus(this)"
function txt_focus(the_form)
{
	if(the_form.value == the_form.defaultValue)
	{
		the_form.value = "";
	}
}
//how to call: onblur="txt_blur(this)"
function txt_blur(the_form)
{
	if(the_form.value == "")
	{
		the_form.value = the_form.defaultValue;
	}
}
function show_options(id, newClass)
{
	var obj = document.getElementById(id);
	obj.className = newClass;
}
function trim( as_data )
{
	return as_data.replace( /^\s+/g, '' ).replace( /\s+$/g, '' );
}

function printit() 
{
	if (window.print)
		window.print();
	else
		showPopWin('', 510, 200);
}
// ------ detect browsers & OS ------ //
function browsers_detector()
{
	var browsers_type = navigator.userAgent.toLowerCase();
	var browsers_name = navigator.appName.toLowerCase();
	var browsers_version = navigator.appVersion.toLowerCase();
	var the_browser;
	
	if(is_string_exist(browsers_type,'opera'))			{the_browser = 'Opera';}
	else if(is_string_exist(browsers_type,'firefox'))	{the_browser = 'Firefox';}
	else if(is_string_exist(browsers_type,'konqueror'))	{the_browser = 'Konqueror';}
	else if(is_string_exist(browsers_type,'safari'))	{the_browser = 'Safari';}
	else if(is_string_exist(browsers_type,'omniweb'))	{the_browser = 'Omniweb';}
	else if(is_string_exist(browsers_type,'webtv'))		{the_browser = 'Webtv';}
	else if(is_string_exist(browsers_type,'icab'))		{the_browser = 'Icab';}
	else if(is_string_exist(browsers_name,'microsoft internet explorer') && is_string_exist(browsers_type,'msie'))	//IE
	{
		the_browser = 'Internet Explorer';
	}
	else if(is_string_exist(browsers_name,'netscape'))	//netscape
	{
		if(is_string_exist(browsers_name,'netscape') && is_string_exist(browsers_type,'netscape'))	//new netscape
		{
			the_browser = 'New Netscape';
		}
		else if(is_string_exist(browsers_name,'netscape') && is_string_exist(browsers_type,'gecko'))	//mozilla
		{
			the_browser = 'Mozilla';
		}
		else	//old netscape
		{
			the_browser = 'Old Netscape';
		}
	}
	else{the_browser = 'Unknown Browser';}
	
	return the_browser;
}

function OS_detector()
{
	var os_type = navigator.userAgent.toLowerCase();
	var the_OS;
	
	if(is_string_exist(os_type,'linux'))	{the_OS = 'Linux';}
	else if(is_string_exist(os_type,'x11'))	{the_OS = 'Unix';}
	else if(is_string_exist(os_type,'mac'))	{the_OS = 'Mac';}
	else if(is_string_exist(os_type,'win'))	{the_OS = 'Windows';}
	else{the_OS = 'Unknown OS';}
	
	return the_OS;
}

function is_string_exist(string,search_string)
{
	search_result = string.indexOf(search_string) + 1;	//indexof: not exist: -1, exist: show the position
	return search_result;
}

// ------ browser & OS variable ------
var detbrowser = browsers_detector();
var decOS = OS_detector();

// ------ detect browsers end ------//

//eg.http://www.pcusa.com/asp/index.asp
//http:// will be eliminated
//link_array[0] will return www.pcusa.com
function locate_url(url_index)
{
	var current_url = window.location + '';
	var url_pair = current_url.split('//');

	if (url_pair.length > 1) 
	{
		current_url = url_pair[1];
	}
	var dir_array = current_url.split('/');
	var link_array = dir_array.slice(0);

	if(link_array[url_index] == null)
	{
		return "";	
	}
	else
	{
		return link_array[url_index];
	}
}

function bookmarkus()
{
	var url_part = locate_url(1);
	var title = parent.document.title;
	
	if(url_part == "" || url_part == "index.html")	//home page
	{
		var title = "PrintingCenterUSA.com | Amazing low price and fast service";
	}

	var url = parent.document.location;
	
	if (window.sidebar)	// for firefox
	{
		window.sidebar.addPanel(title, url, "");
	}
	else if (window.external)	//for IE
	{
		window.external.AddFavorite(url, title);
	}
	else
	{
		showPopWin('', 510, 200);	
	}
}

function bookmarkhome()
{
	var url = "";
	var title = "PrintingCenterUSA.com | Amazing low price and fast service";
	
	if (window.sidebar)	// for firefox
	{
		window.sidebar.addPanel(title, url, "");
	}
	else if (window.external)	//for IE
	{
		window.external.AddFavorite(url, title);
	}
	else
	{
		alert("Sorry, your browser does not support this function.");	
	}
}

function cursors(theCursor)
{
	document.body.style.cursor = theCursor;	
}

/*************************************************************************
  This code is from Dynamic Web Coding at http://www.dyn-web.com/
  See Terms of Use at http://www.dyn-web.com/bus/terms.html
  regarding conditions under which you may use this code.
  This notice must be retained in the code as is!
*************************************************************************/

function getDocHeight(doc) {
  var docHt = 0, sh, oh;
  if (doc.height) docHt = doc.height;
  else if (doc.body) {
    if (doc.body.scrollHeight) docHt = sh = doc.body.scrollHeight;
    if (doc.body.offsetHeight) docHt = oh = doc.body.offsetHeight;
    if (sh && oh) docHt = Math.max(sh, oh);
  }
  return docHt;
}

function setIframeHeight(iframeName) {
  var iframeWin = window.frames[iframeName];
  var iframeEl = document.getElementById? document.getElementById(iframeName): document.all? document.all[iframeName]: null;
  if ( iframeEl && iframeWin ) {
    iframeEl.style.height = "auto"; // helps resize (for some) if new doc shorter than previous  
    var docHt = getDocHeight(iframeWin.document);
    // need to add to height to be sure it will all show
    if (docHt)
	{
		if(detbrowser == "Internet Explorer" && decOS == "Windows")
		{
			iframeEl.style.height = docHt + (-10) + "px";
		}
		else
		{
			iframeEl.style.height = docHt + 8 + "px";	
		}
	}
  }
}

function loadIframe(iframeName, url) {
  if ( window.frames[iframeName] ) {
    window.frames[iframeName].location = url;   
    return false;
  }
  else return true;
}
function goSetHeight(iframe_name) {
  if (parent == window) return;
  // no way to obtain id of iframe object doc loaded into? no parentNode or parentElement or ...
  else parent.setIframeHeight(iframe_name);
}

//save function as indexOf in JS, but safari won't work, so need to make this one
//this one used in presentation folder quote js
//how to call: var the_index = index_of(QA, 2500) will return the index of 2500 in array QA
function index_of(the_array, the_value)
{
	for (the_index in the_array)
	{
		if (the_array[the_index] == the_value)
		{
			break;
		}
	}
	return the_index;
}

//----------------- smooth scrolling v0.3 -----------------
var tinyScrolling = {
	speed : 50,      //set here the scroll speed: when this value increase, the speed decrease. 
	maxStep: 150,	 //set here the "uniform motion" step for long distances
	brakeK: 3,		 //set here the coefficient of slowing down
	hash:null,		
	currentBlock:null,
	requestedY:0,
	init: function() {
			var lnks = document.getElementsByTagName('a');   
			for(var i = 0, lnk; lnk = lnks[i]; i++) {   
				if ((lnk.href && lnk.href.indexOf('#') != -1) &&  ( (lnk.pathname == location.pathname) ||
				('/'+lnk.pathname == location.pathname) ) && (lnk.search == location.search)) {  
				lnk.onclick = tinyScrolling.initScroll;   		
				}   
			}
	},
	getElementYpos: function(el){
			var y = 0;
			while(el.offsetParent){  
				y += el.offsetTop    
				el = el.offsetParent;
			}	return y;
	},		
	getScrollTop: function(){
			if(document.all) return (document.documentElement.scrollTop) ? document.documentElement.scrollTop : document.body.scrollTop;
			else return window.pageYOffset;   
	},	
	getWindowHeight: function(){
			if (window.innerHeight)	return window.innerHeight;
			if(document.documentElement && document.documentElement.clientHeight) return document.documentElement.clientHeight;
	},
	getDocumentHeight: function(){
			if (document.height) return document.height;
			if(document.body.offsetHeight) return document.body.offsetHeight;
	},
	initScroll: function(e){
			var targ;  
			if (!e) var e = window.event;
			if (e.target) targ = e.target;
			else if (e.srcElement) targ = e.srcElement;   
			tinyScrolling.hash = targ.href.substr(targ.href.indexOf('#')+1,targ.href.length); 
			tinyScrolling.currentBlock = document.getElementById(tinyScrolling.hash);   
			if(!tinyScrolling.currentBlock) return;
			tinyScrolling.requestedY = tinyScrolling.getElementYpos(tinyScrolling.currentBlock); 
			tinyScrolling.scroll();  
			return false;
	},
	scroll: function(){
			var top  = tinyScrolling.getScrollTop();
			if(tinyScrolling.requestedY > top) {  
				var endDistance = Math.round((tinyScrolling.getDocumentHeight() - (top + tinyScrolling.getWindowHeight())) / tinyScrolling.brakeK);
				endDistance = Math.min(Math.round((tinyScrolling.requestedY-top)/ tinyScrolling.brakeK), endDistance);
				var offset = Math.max(2, Math.min(endDistance, tinyScrolling.maxStep));
			} else { var offset = - Math.min(Math.abs(Math.round((tinyScrolling.requestedY-top)/ tinyScrolling.brakeK)), tinyScrolling.maxStep);
			} window.scrollTo(0, top + offset);  
			if(Math.abs(top-tinyScrolling.requestedY) <= 1 || tinyScrolling.getScrollTop() == top) {
				window.scrollTo(0, tinyScrolling.requestedY);
				if(!document.all || window.opera) location.hash = tinyScrolling.hash;
				tinyScrolling.hash = null;
			} else 	setTimeout(tinyScrolling.scroll,tinyScrolling.speed);
	}		
}