/**
 * COMMON DHTML FUNCTIONS
 * These are handy functions I use all the time.
 *
 * By Seth Banks (webmaster at subimage dot com)
 * http://www.subimage.com/
 *
 * Up to date code can be found at http://www.subimage.com/dhtml/
 *
 * This code is free for you to use anywhere, just keep this comment block.
 */

/**
 * X-browser event handler attachment and detachment
 * TH: Switched first true to false per http://www.onlinetools.org/articles/unobtrusivejavascript/chapter4.html
 *
 * @argument obj - the object to attach event to
 * @argument evType - name of the event - DONT ADD "on", pass only "mouseover", etc
 * @argument fn - function to call
 */
function addEvent(obj, evType, fn){
 if (obj.addEventListener){
    obj.addEventListener(evType, fn, false);
    return true;
 } else if (obj.attachEvent){
    var r = obj.attachEvent("on"+evType, fn);
    return r;
 } else {
    return false;
 }
}
function removeEvent(obj, evType, fn, useCapture){
  if (obj.removeEventListener){
    obj.removeEventListener(evType, fn, useCapture);
    return true;
  } else if (obj.detachEvent){
    var r = obj.detachEvent("on"+evType, fn);
    return r;
  } else {
    alert("Handler could not be removed");
  }
}

/**
 * Code below taken from - http://www.evolt.org/article/document_body_doctype_switching_and_more/17/30655/
 *
 * Modified 4/22/04 to work with Opera/Moz (by webmaster at subimage dot com)
 *
 * Gets the full width/height because it's different for most browsers.
 */
function getViewportHeight() {
	if (window.innerHeight!=window.undefined) return window.innerHeight;
	if (document.compatMode=='CSS1Compat') return document.documentElement.clientHeight;
	if (document.body) return document.body.clientHeight; 

	return window.undefined; 
}
function getViewportWidth() {
	var offset = 17;
	var width = null;
	if (window.innerWidth!=window.undefined) return window.innerWidth; 
	if (document.compatMode=='CSS1Compat') return document.documentElement.clientWidth; 
	if (document.body) return document.body.clientWidth; 
}

/**
 * Gets the real scroll top
 */
function getScrollTop() {
	if (self.pageYOffset) // all except Explorer
	{
		return self.pageYOffset;
	}
	else if (document.documentElement && document.documentElement.scrollTop)
		// Explorer 6 Strict
	{
		return document.documentElement.scrollTop;
	}
	else if (document.body) // all other Explorers
	{
		return document.body.scrollTop;
	}
}
function getScrollLeft() {
	if (self.pageXOffset) // all except Explorer
	{
		return self.pageXOffset;
	}
	else if (document.documentElement && document.documentElement.scrollLeft)
		// Explorer 6 Strict
	{
		return document.documentElement.scrollLeft;
	}
	else if (document.body) // all other Explorers
	{
		return document.body.scrollLeft;
	}
}


// add2cart Function used to display submodal dialog
function add2cart(part_num) {
	showPopWin('add2cart.php?part_num=' + part_num, 600, 400, null, true);
	//return false;

}

// allows only numbers to be typed into a box ( also delete, backspace, and arrow keys
function CheckNum(e) {

	 return ((e.keyCode == 9) ||(e.keyCode == 46) || (e.keyCode == 8) || (e.keyCode > 36 && e.keyCode <41) || (e.keyCode > 47 && e.keyCode <58) || (e.keyCode > 95 && e.keyCode <106)); 

}

// allows only numbers to be typed into a box ( also delete, backspace, and arrow keys, Enter will submit the gotopage form
function CheckNumGoToPage(e) {

	if (e.keyCode == 13)
	{
		manual_goto();
	} else {
		 return ((e.keyCode == 9) ||(e.keyCode == 46) || (e.keyCode == 8) || (e.keyCode > 36 && e.keyCode <41) || (e.keyCode > 47 && e.keyCode <58) || (e.keyCode > 95 && e.keyCode <106)); 
	}

}

// Used on the checkout / send order page to copy the billing information to the te shipping information fields

function same_addr_onclick() 
		{
			theform = document.form1;
			if (theform.same.checked) {
			theform.ship_comp.value = theform.bill_comp.value;
			theform.ship_name.value = theform.bill_name.value;
			theform.ship_addr.value = theform.bill_addr.value;
			theform.ship_city.value = theform.bill_city.value;
			theform.ship_zip.value = theform.bill_zip.value;
			theform.ship_state.selectedIndex = theform.bill_state.selectedIndex;
			theform.ship_phone.value = theform.bill_phone.value;
			theform.ship_ext.value = theform.bill_ext.value;
			theform.ship_fax.value = theform.bill_fax.value;
			}else{
			theform.ship_comp.value = "";
			theform.ship_name.value = "";
			theform.ship_addr.value = "";
			theform.ship_city.value = "";
			theform.ship_zip.value = "";
			theform.ship_state.selectedIndex = theform.bill_state.selectedIndex;
			theform.ship_phone.value = "";
			theform.ship_ext.value = "";
			theform.ship_fax.value = "";
			
			}
			
			return false;
		}


// valid checks all data on submittion of the checkout page

function valid() {
	//billing validation
	theform = document.form1;
	var good_email = new RegExp("^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9-]+)*$");
	var good_zip = new RegExp("^[0-9]{5}$");
	
	var good_phone = new RegExp("[0-9]{10}");
	var good_phone2 = new RegExp("[0-9]{3}-[0-9]{3}-[0-9]{4}");
	var good_phone3 = new RegExp("\\([0-9]{3}\\)[0-9]{3}-[0-9]{4}");
   
		//email validation
	if (!good_email.test(theform.email.value)) {
        alert( "Please enter a valid Email Address\nPress OK to make this change." );
        theform.email.style.backgroundColor="#33FF00";
		theform.email.focus();
        return false ;
    }

	/*if (theform.bill_comp.value == "") {
        alert( "Please enter a Bill To Company Name.\nPress OK to make this change." );
        theform.bill_comp.style.backgroundColor="#33FF00";
		theform.bill_comp.focus();
        return false ;
    }*/
	
	if (theform.bill_name.value == "") {
        alert( "Please enter a Contact Name.\nPress OK to make this change." );
        theform.bill_name.style.backgroundColor="#33FF00";
		theform.bill_name.focus();
        return false ;
    }
	if (theform.bill_addr.value == "") {
        alert( "Please enter your Address.\nPress OK to make this change." );
        theform.bill_addr.style.backgroundColor="#33FF00";
		theform.bill_addr.focus();
        return false ;
    }
	if (theform.bill_city.value == "") {
        alert( "Please enter your City.\nPress OK to make this change." );
        theform.bill_city.style.backgroundColor="#33FF00";
		theform.bill_city.focus();
        return false ;
    }
	if (theform.bill_state.value == "none") {
        alert( "Please your State.\nPress OK to make this change." );
        theform.bill_state.style.backgroundColor="#33FF00";
		theform.bill_state.focus();
        return false ;
    }

	// US Zip Code
	if (!good_zip.test(theform.bill_zip.value)) {
		alert( "Please your Zip Code.\nOnly 5 digits are allowed.\nPress OK to make this change." );
		theform.bill_zip.style.backgroundColor="#33FF00";
		theform.bill_zip.focus();
		return false;
	}
	// Phone number
	 if ((!good_phone.test(theform.bill_phone.value)) && (!good_phone2.test(theform.bill_phone.value)) && (!good_phone3.test(theform.bill_phone.value))) {
		alert( "Phone number must 10 digits or more.\nAcceptable formats are:\n1235551212\n123-555-1212\n(123)555-1212\nPress OK to make this change." );
        theform.bill_phone.style.backgroundColor="#33FF00";
		theform.bill_phone.focus();
        return false ;
    }
	
}


function reSetBKColor() {
   var ele = document.form1.elements;
   for(var i = 0; i <ele.length;i++) {
    element = ele[i];
    if (element.type) {
      switch (element.type) {
        case 'checkbox':
        case 'text':
        case 'textarea':
        case 'select-one':
			element.style.backgroundColor="";
           //attachEvent(eve,element,func);
       }
     }
  }
}

function ToggleAccount() {
	var grayborder = "solid 1px #999999";
	var disabledcolor = "#cccccc";

	theform = document.form1;
	thediv = document.getElementById("div_create_account");

	if (theform.acct_create[0].checked)
	{
		//create an account
		thediv.style.display="block";
	} else {
		// Do NOT create an account
			thediv.style.display="none";	
	}


}

function valid_account() {
	theform2 = document.acct_form;
	var good_email = new RegExp("^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9-]+(\\.[A-Za-z0-9-]+)*$");
	
	//email validation
	if (!good_email.test(theform2.login_email.value)) {
        alert( "Please enter a valid Email Address\nPress OK to make this change." );
        theform2.login_email.style.backgroundColor="#33FF00";
		theform2.login_email.focus();
        return false ;
    }

	if (theform2.login_pass.value == "") {
        alert( "Please enter your password.\nPress OK to make this change." );
        theform2.login_pass.style.backgroundColor="#33FF00";
		theform2.login_pass.focus();
        return false ;
    }
	
}

function manual_goto() {
	var thebox = document.getElementById("txt_goto_page");
	var proceed = true;
	
	
	if ((thebox.value == "") || (thebox.value == "Page#"))
	{
		alert("Please enter a page number.");
		proceed = false;
	} else {
		flippingBook.getFlippingBookReference().flipGotoPage(parseInt(thebox.value)+pageOffset);
		thebox.value = "Page#";
	}
}

function clearpagebox() {
	var thebox = document.getElementById("txt_goto_page");
	if (thebox.value == "Page#")
	{
		thebox.value = "";
	}

}
