/* Global JavaScript library for OCDLA.org */

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function MM_preloadImages() { //v3.0
  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];}}
}

	
function DW_updateprice(x) { //v1.0
	var startingtotal = parseFloat(document.forms.orderform.Total.value);
	var optionprice = parseInt(x.value);
	if (x.checked==true) { //then they're checking the box and the price should be increased
		var newtotal = currencyformat(startingtotal+optionprice);
	}
	else if (x.checked==false) { //then they're un-checking the box and the price should be decreased
		var newtotal = currencyformat(startingtotal-optionprice);
	}
	document.forms.orderform.Total.value = newtotal;
	document.forms.orderform.DisplayTotal.value = newtotal;
}
	
	
function currencyformat(inputnumber) { //v1.0
	var inputstring = new String(inputnumber);
		if (inputstring.length==(inputstring.indexOf(".")+3)) {
			outputstring = inputstring;
		}
		if (inputstring.length>(inputstring.indexOf(".")+3)) {
			outputstring = inputstring.substr(0,(inputstring.indexOf(".")+3));
		}
		if ((inputstring.length-inputstring.indexOf("."))==2) {
			outputstring = inputstring+"0";
		}
		if (inputstring.indexOf(".")==-1) {
			outputstring = inputstring+".00";
		}
	return outputstring;
}
	
	
function DW_TimeStampIt() { //v1.0
	var myTimeStamp = new Date();
	document.forms.orderform.TimeStamp.value = myTimeStamp.getTime();
}


function setfocus(){
	document.Form.UserName.focus();
	 return true; 
}

var signon = true;

function send(){
 	if(document.Form.UserName.value == "" && signon){
        alert("Please enter your OCDLA Username.\n\nIf you've forgotten your OCDLA Username,\nPlease contact OCDLA.");
        document.Form.UserName.focus();
        document.Form.UserName.select();
        return false;
 	}

	return true;
	
}

function clearForm(){
	elems=document.forms[0].elements;
	arr=new Array();
	//window.alert(document.forms[0].elements);
	//loop through quantities and assign 0 values, where appropriate
	for(i=0; i<elems.length; i++){
		//window.alert(elems[i].name+": "+elems[i].value);
		if(elems[i].name=="quantity") elems[i].value="";
	}//
	

	return true;  
}//clearForm

function checkForm(){
	elems=document.forms[0].elements;
	arr=new Array();
	//window.alert(document.forms[0].elements);
	//loop through quantities and assign 0 values, where appropriate
	try {
	for(i=0; i<elems.length; i++){
		//window.alert(elems[i].name+": "+elems[i].value);
		if(elems[i].name=="quantity" && elems[i].value=="") elems[i].value=0;
		if(elems[i].name=="quantity" && elems[i].value=="0") elems[i].value=0;
	}//
	} catch(e){ throw "Unable to reset default quantity to 0."; }
	

	return true;  
}//checkForm



var CartLibrary = {
	hello : function(){ alert(location.hostname); },
	loginurl : location.hostname+"/includes/evaluateLoginXML.php",
	zipgenerationurl : location.hostname+"/send-pdfs/generateorder_createZip.php",
	
	getLoginWidget : function(ajaxobj) {
	// next commented line is the original function declaration
//function toConsole(data){
	var data = ajaxobj.req.responseXML;
	var navlinks = document.getElementById('navlinks');
	var logout_url = location.protocol+"//"+location.hostname+"/logout.php";
	xmlDoc = data.documentElement;	
	

	
	/**
	 * 		* find out if the user is logged in or not
	 * if not then exit
	 */
	loggedin = xmlDoc.getElementsByTagName('loggedin');
	loggedin = loggedin[0].firstChild.nodeValue;
	if( loggedin != "true" ) { return false; }
	
	
	
	/*
	  * below we can pull various nodes from our xml tree *
	   *  **
	  */	
	firstname = xmlDoc.getElementsByTagName('firstname');
	firstname = firstname[0].firstChild.nodeValue;


	txt_firstname = document.createTextNode( firstname );
	elem_firstname = document.createElement( 'span' );
	elem_firstname.setAttribute('id','login-firstname');
	elem_firstname.appendChild( txt_firstname );
	
	/*
	  *** create the logout button since the user is logged in
	  *
	   */
	   
	   link_to_profile = document.createElement('a');
	   link_to_profile.setAttribute('href','/profiles/index.php');
	   link_to_profile.appendChild( document.createTextNode( 'Hello, '+firstname ) );
	   		
		logout = document.createElement('a');
		logout.setAttribute('href', logout_url);
		logout.appendChild(document.createTextNode('Logout'));//ajaxobj.req.responseText ));

		user_widget=document.getElementById('user-widget');	
		user_widget.appendChild( document.createTextNode(' | ') );
		user_widget.appendChild( link_to_profile );
		user_widget.appendChild( document.createTextNode(' | ') );
		user_widget.appendChild( logout );
		
		removeElementById( 'login-block' );//remove the login button if the user has authenticated
	

},//getLoginWidget


getCookie : function(NameOfCookie) {

// First we check to see if there is a cookie stored.
// Otherwise the length of document.cookie would be zero.

if (document.cookie.length > 0)
{

// Second we check to see if the cookie's name is stored in the
// "document.cookie" object for the page.

// Since more than one cookie can be set on a
// single page it is possible that our cookie
// is not present, even though the "document.cookie" object
// is not just an empty text.
// If our cookie name is not present the value -1 is stored
// in the variable called "begin".

begin = document.cookie.indexOf(NameOfCookie+"=");
if (begin != -1) // Note: != means "is not equal to"
{

// Our cookie was set.
// The value stored in the cookie is returned from the function.

begin += NameOfCookie.length+1;
end = document.cookie.indexOf(";", begin);
if (end == -1) end = document.cookie.length;
return unescape(document.cookie.substring(begin, end)); }
}
return null;

// Our cookie was not set.
// The value "null" is returned from the function.

}//getCookie


}//CartLibrary

var Feed = {
	getFeed: function(ajaxobj){
		var feed_elem = document.getElementById('feed');
		feed_elem.innerHTML = ajaxobj.req.responseText;
	}
}


	var protocol=/https:\/\//gi;
	var www=/www/;
	var hostname, redirect;
	
	function wwwsecure(){
	if(window.location.protocol!="https:" || window.location.hostname.search(www)==-1) {
		hostname=window.location.hostname.search(www)==-1?"www."+window.location.hostname:window.location.hostname; 
		redirect="https://"+hostname+window.location.pathname;
		window.location=redirect;
	}//if
	}//wwwsecure


function init(){
	// quit if this function has already been called
	if (arguments.callee.done) return;
	
	// flag this function so we don't do the same thing twice
	arguments.callee.done = true;
	
	// do stuff
	P7_PMMop('p7PMM_1',0,1,-5,-5,0,0,0,1,0,3,1,1,0);
	P7_PMMinit();//revised 2010-02-09
		login = document.getElementById('login');
		//check here to see if we are appending an additional querystring value or adding the query string
		// i.e., check for a ? in the querystring
		login.href += login.href.indexOf('?')>0 ? "&" : "?";  login.href +='ref='+location.pathname;
}//init


   /* for Mozilla */
   if (document.addEventListener) {
       document.addEventListener("DOMContentLoaded", init, false);
   }

   /* for other browsers */
   window.onload = init;

/*if (window.attachEvent) window.attachEvent("onload", init);
else window.onload = init;*/



function removeElementById( elem_id ) {
	try {
		elem = document.getElementById( elem_id );
		p=elem.parentNode;
		p.removeChild( elem );

	} catch(e) { throw 'removeElementById: element with id='+elem_id+' not found.'; }

}//removeElementById
