	$(document).ready(function() {
	
	// Expand Panel
	$("#spsignin").click(function(){
		signin_move();
		$("div#signin_floatcontainer").toggle();
	});	
	
	// Collapse Panel
//	$("#signing").click(function(){
//		$("div#signin_floatcontainer").slideUp("slow");	
//	});		
	
	// Switch buttons from "Log In | Register" to "Close Panel" on click
//	$("#toggle a").click(function () {
//		$("#toggle a").toggle();
//	});		
		
});

function signin_move() {
		
	if (navigator.appName.indexOf("Microsoft")!=-1) {
		winW = document.body.offsetWidth;
		//alert(winW);
		if (winW > 1008) {
			winW = (winW - 24);
		} else {
			winW = (winW - 148);
			alert('boo');
		}
	} else {
		winW = window.innerWidth;
	}
	
	if (winW > 1009) {
		document.getElementById('signin_floatcontainer').style.left =  (((winW - 1009) / 2) + 730 ) +'px';
	} else {
		document.getElementById('signin_floatcontainer').style.left = (1009 - 270) + 'px';
	}

}

function doesNothing() { return true; }

function fnToggleDiv(vObj, objname){
	var aDivs = document.getElementsByTagName("div");
//		document.getElementById(objname).style.left = vObj.style.left';
//		document.getElementById(objname).style.top =  vObj.style.top+'px';
	
	if(document.getElementById(objname).style.visibility == 'hidden'){
		//Show the div
		document.getElementById(objname).style.visibility='visible';	
	} else {
		// div is not hidden, so hide it
		document.getElementById(objname).style.visibility='hidden';
	}
}

function fnshowhide(vId, vTag) {
	
	vlen = vTag.length;
	vStatus = document.getElementById(vTag+'box'+vId).style.display;
	coll = document.getElementsByTagName('div');
	
	if ( coll != null ) {
		for ( i=0; i < coll.length; i++ ) {
			if (vTag+'box' == coll.item(i).id.substring(0,(vlen+3))) {
			coll.item(i).style.display = 'none';
			}
		if (vTag+'click' == coll.item(i).id.substring(0,(vlen+5))) { 
			coll.item(i).style.color = '#78A330';
			coll.item(i).innerHTML = '[Show]';
			}
		}
	if(vStatus == 'none'){	
		// div is hidden, so let's show it
		document.getElementById(vTag+'click'+vId).innerHTML = '[Hide]';
		document.getElementById(vTag+'click'+vId).style.color = '#E27655';
		//Show the div
		document.getElementById(vTag+'box'+vId).style.display='block';
	} else {
		// div is not hidden, so hide it
		document.getElementById(vTag+'click'+vId).innerHTML = '[Show]';
		document.getElementById(vTag+'click'+vId).style.color = '#78A330';
		//Show the div
		document.getElementById(vTag+'box'+vId).style.display='none';
		}
	}
}

function recalcPage(vPrefix, vTotalName) {
	var vNum = parseFloat(0);
	coll = document.getElementsByTagName('span');
	if ( coll != null ) {
		for ( i=0; i < coll.length; i++ ) {
			if (vPrefix == coll.item(i).id.substring(0,vPrefix.length)) {
				vNum += parseFloat(coll.item(i).innerHTML);
			}
		}
	}
	document.getElementById(vTotalName).innerHTML = formatMoney(vNum);
}


function formatMoney(vNum)
{
	var i = parseFloat(vNum);
	if(isNaN(i)) { i = 0.00; }
	var minus = '';
	if(i < 0) { minus = '-'; }
	i = Math.abs(i);
	i = parseInt((i + .005) * 100);
	i = i / 100;
	s = new String(i);
	if(s.indexOf('.') < 0) { s += '.00'; }
	if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
	s = minus + s;
	return s;
}


function fnClearSearch() {
	document.getElementById('keyword').value='';
	document.getElementById('company_name').value='';
	document.getElementById('product_collection').value='';
	document.getElementById('select_season_list').options[0].selected = true;
	document.getElementById('product_sku').value='';
	document.getElementById('product_upc').value='';
	document.getElementById('select_color_list').options[0].selected = true;
	
}

function submitonEnter(evt, vObjid){
	var charCode = (evt.which) ? evt.which : event.keyCode
	if(charCode == "13"){
		document.getElementById(vObjid).submit();
		}
	}

function fnAddQtyonEnter(evt, vProduct_id) {
	var charCode = (evt.which) ? evt.which : event.keyCode
	if(charCode == "13"){
		fnaddtoorder(vProduct_id);
		}
	}

function fnShowCategories(vcatnum) {
	coll = document.getElementsByName('select_category_list');
	
	if ( coll != null ) {
		for ( i=0; i < coll.length; i++ ) 
			coll.item(i).style.display = 'none';
		
		}
	
	document.getElementById('select_category_list_'+vcatnum).style.display = 'inline';
}


function fnLoadAccountTestData() {
	
	//company_name: bytestudio
	//company_store_type: -1
	//contact_first_name: Ric
	//contact_last_name: Colgan
	//contact_email: ric@moreturns.com
	//contact_phone: 2006-855-9223
	//company_street_address: 10396 NE South Beach DR
	//company_city: Bainbridge Island
	//company_state: WA
	//company_zip: 98110
	//tc_accepted: on
	//sales_rep_email: rep@moreturns.com
	//sales_rep_code: codeisgod
	//register: Create Account
	//PHPSESSID: e348cb10e5efbc988f2d8c1ea8a9fb51
	
    document.getElementById("company_name").value = 'more turns, llc.';
    document.getElementById("retailer_type").selectedIndex = 2;
    document.getElementById("contact_first_name").value = '';
	document.getElementById("contact_last_name").value = '';
	document.getElementById("contact_email").value = '';
	document.getElementById("contact_phone").value = '';
	document.getElementById("company_street_address1").value = '';
	document.getElementById("company_street_address2").value = '';
	document.getElementById("company_city").value = '';
	document.getElementById("company_state").value = '';
	document.getElementById("company_zip").value = '';
	document.getElementById("tc_accepted").checked = true;
	document.getElementById("sales_rep_email").value = '';
	document.getElementById("sales_rep_code").value = '';
	document.getElementById("contact_password").value = '';
	document.getElementById("contact_password2").value = '';
	
	}

function ajaxKillImage(vProduct, vUser)
{
var xmlHttp;
var params = '';

try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    try
      {
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    catch (e)
      {
      alert("Your browser does not support AJAX!");
      return false;
      }
    }
  }
  xmlHttp.onreadystatechange=function()
    {
    if(xmlHttp.readyState==4)
      {
      //document.getElementById("divphotoupdate"+vInt).innerHTML='[done]';
      }
    }
    
    params = "product_id=" + encodeURI(vProduct)+
		"&user_id=" + encodeURI(vUser);
//alert(params);
	xmlHttp.open("POST","../imagekill.php",true);
	//Send the proper header information along with the request
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(params);
	}

function ajaxFindCompany(vCompany)
{
var xmlHttp;
var params = '';

if (vCompany.length > 3) {

try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    try
      {
      xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    catch (e)
      {
      alert("Your browser does not support AJAX!");
      return false;
      }
    }
  }
  xmlHttp.onreadystatechange=function()
    {
    if(xmlHttp.readyState==4)
      {
      document.getElementById("select_company_name").innerHTML= xmlHttp.responseText;
      }
    }
    
    params = "company_name=" + encodeURI(vCompany);
//alert(params);
	xmlHttp.open("POST","../findcompany.php",true);
	//Send the proper header information along with the request
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttp.setRequestHeader("Content-length", params.length);
	xmlHttp.setRequestHeader("Connection", "close");
	xmlHttp.send(params);
	}
}

var minimum = 8; 
var fair = 8; 
var strength_label = Array( 'Min. 8 Chars', 'Incomplete', 'Incomplete', 'Incomplete', 'Good' ); 
var strength_color = Array( '#FF0000', '#FF9900', '#FFCC33', '#99CC99', '#00CC33' ); 

function testpassword( pwInput, pwSpan ) { 
	if(!pw){ var pw = document.getElementById(pwInput).value.toString(); } 
	if(!pw){ return false; } 
	var strength = 0; 
	if( pw.length >= minimum ) { 
		strength = 1; 
		if(pw.length >= fair){ 
			strength++; 
			} 
		if((/\d+/.test(pw)) || (/\W+/.test(pw))){ 
			strength++; 
			} 
		if(/[a-z]+/.test(pw) || /[A-Z]+/.test(pw)){ 
			strength++; 
			} 
		} 
	document.getElementById(pwSpan).innerHTML = strength_label[ strength ]; 
	document.getElementById(pwSpan).style.color = strength_color[ strength ];  
	} 

function comparepassword( pw1Input, pw2Input, pwSpan, pwdButton) { 
	if(!pw1){ var pw1 = document.getElementById(pw1Input).value.toString(); }
	if(!pw2){ var pw2 = document.getElementById(pw2Input).value.toString(); }
	if(!pw1){ return false; }
	if(!pw2){ return false; }	 
	if(( pw1 == pw2) && (pw2.length > 0)) { 
		document.getElementById(pwSpan).innerHTML = 'Match!';
		document.getElementById(pwSpan).style.color = '#00CC33';
		var strength = 0; 
		if( pw2.length >= minimum ) { 
			strength = 1; 
			if(pw2.length >= fair){ 
				strength++; 
				} 
			if((/\d+/.test(pw2)) || (/\W+/.test(pw2))){ 
				strength++; 
				} 
			if(/[a-z]+/.test(pw2) || /[A-Z]+/.test(pw2)){ 
				strength++; 
				} 
			}
			if (strength > 3) {
			document.getElementById(pwdButton).disabled = false;
			}
		} else {
		document.getElementById(pwSpan).innerHTML = 'Do Not Match';
		document.getElementById(pwSpan).style.color = '#CC3300';
		document.getElementById(pwdButton).disabled = true;
		}	
	  
	} 

function inputCounter( field, countfield, maxlimit ) 
{
if ( field.value.length > maxlimit )
	{
	field.value = field.value.substring( 0, maxlimit );
	alert( 'Text entry can only be ' + maxlimit + ' characters in length.' );
	countfield.style.color = 'red';
	countfield.innerHTML = maxlimit - field.value.length;
	return false;
	}
else
	{
	if ((maxlimit - field.value.length) < 100)
		{
		countfield.style.color = 'red';
		countfield.innerHTML = maxlimit - field.value.length;
		}
	else
		{
		countfield.style.color = 'green';
		countfield.innerHTML = maxlimit - field.value.length;
		}
	}	
}

function doMoveBox(event, vboxname) { 
	if( typeof( window.pageYOffset ) == 'number' ) {
		//Netscape compliant
		document.getElementById(vboxname).style.left = (event.clientX+40)+'px';
		document.getElementById(vboxname).style.top = (window.pageYOffset+event.clientY-160)+'px';
	} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
		//DOM compliant
		document.getElementById(vboxname).style.left = (event.clientX+40)+'px';
		document.getElementById(vboxname).style.top = (document.body.scrollTop+event.clientY-160)+'px';
	} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
		//IE6 standards compliant mode
		document.getElementById(vboxname).style.left = (event.clientX+40)+'px';
		document.getElementById(vboxname).style.top = (document.documentElement.scrollTop+event.clientY-160)+'px';
	}
}


function fnSearchUpd(vProduct_id) {
	var vQty = document.getElementById('itemqty'+vProduct_id).value;
	;
	if ((vQty < 0) || (isNaN(vQty) == true)) {
		alert('Please enter a valid quantity to order')
	} else {
		document.body.style.cursor = 'wait'
		$.post("../api/acartupd.php", { product_id: vProduct_id, qty: vQty },
			function(data){
			//document.getElementById("ajaxdebug").innerHTML = data+'<br/>';
			document.getElementById('itemqty'+vProduct_id).style.color = '#000000';
			aResults = data.split("|");
			document.getElementById('tbcart').innerHTML = aResults[1];
			document.body.style.cursor='auto';
			}, "text");
	}
}
	


function fnCartPageDel(vRow_id, vProduct_id, vPlace) {
	var vprodsku = document.getElementById('cartsku'+vProduct_id).value;
	vRow_id = encodeURI(vRow_id);
	if(confirm('Are you sure you want to remove SKU: ' + vprodsku + ' from your '+vPlace+'?')) {
		document.body.style.cursor = 'wait'
		$.post("../api/acartdel.php", { row_id: vRow_id },
		function(data){
			//document.getElementById("ajaxdebug").innerHTML = data+'<br/>';
			window.location.href = window.location;
			}, "text");
	}
}
		

function fnCartEmpty(vCompany_id) {
	vCompany_id = encodeURI(vCompany_id);
	var vprodco = document.getElementById('cartco'+vCompany_id).value;
	if(confirm('Are you sure you want to empty your cart for '+ vprodco + '?')) {
	document.body.style.cursor = 'wait'
		$.post("../api/acartempty.php", { mfgcompany_id: vCompany_id },
		function(data){
			//document.getElementById("ajaxdebug").innerHTML = data+'<br/>';
			window.location.href = window.location;
			}, "text");
	}
}


function fnSaveOrderNote() {
	var vnote = encodeURI(document.getElementById('order_notes').value);
	var vorder_header_id = encodeURI(document.getElementById('order_header_id').value);
	if ((vnote.length < 1) && (vorder_header_id > 0)) {
		alert('Order Note must have some note text.');
	} else {
	document.body.style.cursor = 'wait'
		$.post("../api/aorder_note.php", { order_note: vnote, order_header_id: vorder_header_id },
		function(data){
			//document.getElementById("ajaxdebug").innerHTML = data+'<br/>';
			document.getElementById("ordernotes").innerHTML = data;
			document.getElementById('order_notes').value = '';
			}, "text");
	}
}



var qTipTag = "div"; //Which tag do you want to qTip-ize? Keep it lowercase!//
var qTipX = 0; //This is qTip's X offset//
var qTipY = 15; //This is qTip's Y offset//

//There's No need to edit anything below this line//
tooltip = {
  name : "qTip",
  offsetX : qTipX,
  offsetY : qTipY,
  tip : null
}

tooltip.init = function () {
	var tipNameSpaceURI = "http://www.w3.org/1999/xhtml";
	if(!tipContainerID){ var tipContainerID = "qTip";}
	var tipContainer = document.getElementById(tipContainerID);

	if(!tipContainer) {
	  tipContainer = document.createElementNS ? document.createElementNS(tipNameSpaceURI, "div") : document.createElement("div");
		tipContainer.setAttribute("id", tipContainerID);
	  document.getElementsByTagName("body").item(0).appendChild(tipContainer);
	}

	if (!document.getElementById) return;
	this.tip = document.getElementById (this.name);
	if (this.tip) document.onmousemove = function (evt) {tooltip.move (evt)};

	var a, sTitle, elements;
	
	var elementList = qTipTag.split(",");
	for(var j = 0; j < elementList.length; j++)
	{	
		elements = document.getElementsByTagName(elementList[j]);
		if(elements)
		{
			for (var i = 0; i < elements.length; i ++)
			{
				a = elements[i];
				sTitle = a.getAttribute("title");				
				if(sTitle)
				{
					a.setAttribute("tiptitle", sTitle);
					a.removeAttribute("title");
					a.removeAttribute("alt");
					a.onmouseover = function() {tooltip.show(this.getAttribute('tiptitle'))};
					a.onmouseout = function() {tooltip.hide()};
				}
			}
		}
	}
}

tooltip.move = function (evt) {
	var x=0, y=0;
	if (document.all) {//IE
		x = (document.documentElement && document.documentElement.scrollLeft) ? document.documentElement.scrollLeft : document.body.scrollLeft;
		y = (document.documentElement && document.documentElement.scrollTop) ? document.documentElement.scrollTop : document.body.scrollTop;
		x += window.event.clientX;
		y += window.event.clientY;
		
	} else {//Good Browsers
		x = evt.pageX;
		y = evt.pageY;
	}
	this.tip.style.left = (x + this.offsetX) + "px";
	this.tip.style.top = (y + this.offsetY) + "px";
}

tooltip.show = function (text) {
	if (!this.tip) return;
	this.tip.innerHTML = text;
	this.tip.style.display = "block";
}

tooltip.hide = function () {
	if (!this.tip) return;
	this.tip.innerHTML = "";
	this.tip.style.display = "none";
}

window.onload = function () {
	tooltip.init ();
}

function fn_clearrepsearch(vObj) {
	
	if (vObj.value == vObj.defaultValue) {
		vObj.style.color = '#000000';
    	vObj.value = '';
    	vObj.maxLength = 5;
 		}
	}

	
function fn_resetrepsearch(vObj) {

	if (vObj.value == "") {
		vObj.style.color = '#78A22E';
		vObj.maxLength = '';
    	vObj.value = 'enter zip code';
 		}
	}		
	
function fn_validaterepsearch(vObj) {
	var objRegExp  = /(^\d{5}$)|(^\d{5}-\d{4}$)/;


	if ((vObj.value.length < 5) || (vObj.value == 'enter zip code')) {
		alert('invalid Zip code.')
		vObj.value="";
		setfocus(vObj);
		return false;
		} 
	if (objRegExp.test(vObj.value) == false) {
		alert('invalid Zip code.')
		vObj.value="";
		setfocus(vObj);
		return false;
		}
	}
	
function fn_validatemainrepsearch(vObj) {
	var objRegExp  = /(^\d{5}$)|(^\d{5}-\d{4}$)/;
		
	if ((document.getElementById('mainrepsearch').value.length < 5) || (document.getElementById('mainrepsearch').value == ' enter zip code ')) {
		alert('You must enter a valid zip code to search.');
		return false;
		setfocus(vObj);
		} 
	if (objRegExp.test(document.getElementById('mainrepsearch').value) == false) {
		alert('invalid Zip code.')
		return false;
		setfocus(vObj);
		}
	}



//--------------------------------------------
//               setfocus
//Delayed focus setting to get around IE bug
//--------------------------------------------

function setFocusDelayed()
{
global_valfield.focus();
}

function setfocus(valfield)
{
// save valfield in global variable so value retained when routine exits
global_valfield = valfield;
setTimeout( 'setFocusDelayed()', 100 );
}

function fnMoveToOrder(vOrder_row_id, vOrder_header_id, vSku, vOrdername) {
	document.body.style.cursor='wait';
	vOrder_row_id = parseInt(vOrder_row_id);
	vOrder_header_id = parseInt(vOrder_header_id);
	if(confirm('Are you sure you want to move SKU: ' + vSku + ' to order: '+vOrdername+'?')) {
		if ((parseInt(vOrder_row_id) > 0) && (parseInt(vOrder_header_id) >= 0)) {
		$.post("../api/moveorderrow.php", { order_row_id: vOrder_row_id, order_header_id: vOrder_header_id  },
			function(data){
			//document.getElementById('ajaxdebug').innerHTML = data;
			document.body.style.cursor='wait';
			window.location.href = window.location;
			}, "text");
		}
	}
}
