/* 5:04 PM 10/14/2009 */ 
/*
Copyright © 2008 Eckx Media Group, LLC. All rights reserved.
Eckx Media Group respects the intellectual property of others, and we ask our users to do the same.
*/
/*<script>*/

// Show / Hide object
function toggle(obj) {
	var el = $(obj);
	el.style.display = (el.style.display != 'block' ? 'block' : 'none' );
	el.blur();
}
function toggle2(obj) {
	var el = $(obj);
	el.style.display = (el.style.display != 'block' ? 'block' : 'none' );
	el.blur();
}

// Reset form fields
function clearForm(id, skipType) {
	var form = document.getElementById(id);;
	for (var i = 0; i < form.length; i++) {
		if(form[i].type == skipType || form[i].type == 'submit' || form[i].type == 'button' ){
			continue;
		}
		clearField(form[i]);
	}
}
// Reset form fieldset fields
function clearFieldset(id) {
	var fieldset = $$('#' + id + ' input[type="text"], ' + '#' + id + ' input[type="password"], ' + '#' + id + ' input[type="file"], ' + '#' + id + ' select, ' + '#' + id + ' textarea');
	
	for (var i = 0; i < fieldset.length; i++) {
		clearField(fieldset[i]);
	}
}
// Clear individual field
function clearField (field) {
	if(field.type == 'checkbox' || field.type == 'radio') {
		field.checked = false;	
	}
	else {
		field.value = '';
	}
}

function popUpA(URL) { //allow all features
day = new Date();
id = "aboutUS";
eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=1,scrollbars=1,location=1,statusbar=1,menubar=1,resizable=1,width=900,height=400,left = 240,top = 212');");
}

function popUpB(URL) { // disable all features
day = new Date();
id = "aboutUS";
eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=0,width=300,height=300,left = 240,top = 212');");
}

function isset(obj){
	if(typeof obj == 'undefined'){
		return false;
	}
	else{
		return true;	
	}
}


function getMousePos(e) {
	var IE = document.all?true:false
	var scrollXY = getScrollXY();
	var mousePos = new Array();
	if (IE) { // grab the x-y pos.s if browser is IE
		tempX = e.x;
		tempY = e.y;
	} 
	else {  // grab the x-y pos.s if browser is NS
		tempX = e.clientX;
		tempY = e.clientY;
	}
	// catch possible negative values in NS4
	if (tempX < 0){tempX = 0}
	if (tempY < 0){tempY = 0}  
	mousePos['x'] = tempX + scrollXY[0];
	mousePos['y'] = tempY + scrollXY[1];
	return mousePos;
}


function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}

function getPageDim(){
	if(document.all?true:false){ // IE
		if(document.body.clientHeight > document.body.scrollHeight){
			var height = document.body.clientHeight;
			var width = document.body.clientWidth;
		}
		else{
			var height = document.body.scrollHeight;
			var width = document.body.scrollWidth;
		}
	}
	else{
		var height = document.height;
		var width = document.weidth;
	}
	var viewPortHeight = document.viewport.getHeight();
	if(height < viewPortHeight){
		height = viewPortHeight;
	}
	return [ width, height ];
}

function getVisibleDim(){ alert('function getVisibleDim() decremented, use prototype viewport');
	if(!$('getTopLeft-fake-body')){ //generate fake div to get screen size
		var fakeDiv = document.createElement('div');
		fakeDiv.id = 'getTopLeft-fake-body';
		fakeDiv.style.visibility = 'hidden';
		fakeDiv.style.margin = '0';
		fakeDiv.style.padding = '0';
		fakeDiv.style.position = 'absolute';
		fakeDiv.style.top = '0';
		fakeDiv.style.bottom = '0';
		fakeDiv.style.left = '0';
		fakeDiv.style.right = '0';
		fakeDiv.style.width = '100%';
		fakeDiv.style.height = '100%';
		fakeDiv.style.zIndex = '-1';
		document.body.appendChild(fakeDiv);
	}
	
	var fakeDiv = $('getTopLeft-fake-body');
	var width = fakeDiv.getWidth();
	var height = fakeDiv.getHeight();
	return [ width, height ];

}

function alert2(text, dim, alertTime, className){ 
	//check if alert 2 already exist
	var i=0;
	while($('alert2_'+i)){
		i++;
	}
	var alert2 = document.createElement('div');
	alert2.id = 'alert2_'+i;
	alert2.style.visibility = 'hidden';
	document.body.appendChild(alert2);
	
	alert2 = $('alert2_'+i);
	if (className === undefined) {
		alert2.addClassName('alert2');
	}
	else {
		alert2.addClassName(className);	
	}
	
	alert2.innerHTML = text;
	if(dim){
		width = dim[0];
		height = dim[1];
		alert2.style.width = width+'px';
		alert2.style.height = height+'px';
	}
	else{
		width = alert2.getWidth();
		height = alert2.getHeight();
	}
	if(isNaN(width) || isNaN(height)){
		alert('Alert2() error, width or height isNaN');	
	}
	
	var xy = getScrollXY(); 
	var topLeft = getTopLeft(width, height);
	alert2.style.top = topLeft[0]+'%';
	alert2.style.left = topLeft[1]+'%';
	alert2.style.visibility = 'visible';
	if(!alertTime){
		alertTime = 2000;	
	}
	setTimeout("document.body.removeChild(document.getElementById('alert2_"+i+"'))", alertTime);
}


//return the top left percentage for an absolute centered layer, req 100% body height
function getTopLeft(width, height){
	//var visibleDim = getVisibleDim();
	//var windowWidth = visibleDim[0];
	//var windowHeight = visibleDim[1];
	document.viewport.getWidth()
	var windowWidth = document.viewport.getWidth();
	var windowHeight = document.viewport.getHeight();
	var ie = getIEVerNum();
	
	//compensate for scroll
	var xy = getScrollXY();
	
	//get %
	var top = (windowHeight/2 + xy[1] - (height/2)) / windowHeight;
	var left = (windowWidth/2 + xy[0] - (width/2)) / windowWidth;

	if(top < 0){
		top = 0;	
	}
	if(left <0){
		left = 0;	
	}
	
	//compensate for ie 6 usage of %, the entire document not just what u see is 100%
	if(ie == 6){ // ie 6
		var pxHeight = windowHeight * top; //get pixel height
		top = pxHeight/document.body.clientHeight; // get decimal height
	}
	
	top  = Math.round(top * 100); 
	left  = Math.round(left * 100);
			
	return [ top, left ];
}

function money(num){
	var formated = Math.round(num*100)/100;
	formated = formated.toString();
	if(formated.indexOf('.') == -1){
		formated += '.00';
	}
	else{
		var parts = formated.split('.');
		if(parts[1].length == 1){
			formated += '0';	
		}
	}
	return formated;
}

function urlencode(str) {
	str = escape(str);
	str = str.replace('+', '%2B');
	str = str.replace('%20', '+');
	str = str.replace('*', '%2A');
	str = str.replace('/', '%2F');
	str = str.replace('@', '%40');
	return str;
}

function urldecode(str) {
	str = str.replace('+', ' ');
	str = unescape(str);
	return str;
}

function htmlentities(html) {
	html = html.replace('<','&lt;');
	html = html.replace('>','&gt;');
	html = html.replace('"','&quot;');
	return html;
} 

function getJs(url){
	if(url.indexOf('?')==-1) {
		url += '?';	
	}
	var jsel = document.createElement('SCRIPT');
	jsel.type = 'text/javascript';
	jsel.src = url+'&klioe='+Math.random()*10000;
	document.body.appendChild(jsel);
}

//Get IE Version Number
function getIEVerNum() {
    var ua = navigator.userAgent;
    var MSIEOffset = ua.indexOf("MSIE ");
    
    if (MSIEOffset == -1) {
        return 0;
    } else {
        return parseFloat(ua.substring(MSIEOffset + 5, ua.indexOf(";", MSIEOffset)));
    }
}

function confirm2(e, title, yesEval, noEval){
	var delConfirm = document.createElement('div');
	delConfirm.id = 'confirm2';
	document.body.appendChild(delConfirm);
	curtain.load();
	curtain.content('<p><strong>'+title+'</strong></p><ul class="tools confirm"><li class="yes"><a href="#" id="confirm2-yes">Yes</a></li><li class="no"><a href="#" id="confirm2-no">No</a></li></ul>');
	//delConfirm = $('confirm2');
	//delConfirm.addClassName('confirm2');
	//delConfirm.innerHTML = '<div>'+title+'</div><input type="button" id="confirm2_yes" value="Yes"/><br/><input type="button" id="confirm2_no" value="No" />';
	
	//var mousePos = getMousePos(e);
	//delConfirm.style.left=mousePos['x']+'px';
	//delConfirm.style.top=mousePos['y']+'px';
	$('confirm2-yes').onclick= function(){ 
		//document.body.removeChild($('confirm2'));
		eval(yesEval);
		curtain.close();
		return false;
	}
	$('confirm2-no').onclick= function(){ 
		//document.body.removeChild($('confirm2'));
		eval(noEval); 
		curtain.close();
		return false;
	}
}

function checkAll(name, trueFalse){
	var checkBoxes = document.getElementsByName(name);
	var len = checkBoxes.length;
	for(var i=0; i<len; i++){
		checkBoxes[i].checked = trueFalse;
	}
}

function externalLinks(container) {
	if(container){
		var anchors = container.select('a[rel*="external"]');
	}
	else{
		var anchors = $$('a[rel*="external"]');
	}
	for (var i=0; i<anchors.length; i++) {
		anchors[i].target = "_blank";
	}
}

function autoCompleteOff(){
	var inputs = $$('input.autocomplete-off');
	for (var i=0; i<inputs.length; i++) {
		inputs[i].setAttribute("autocomplete", "off");
	}
}

function defaultClear(){
	var inputs = $$('input.default-clear');
	for (var i=0; i<inputs.length; i++) {
		inputs[i].onfocus = function(){
			if(this.value == this.defaultValue){
				this.value = ''; 
			}
		}
		inputs[i].onblur = function(){
			if(this.value == ''){
				this.value = this.defaultValue;
			}
		}
	}
}

function emgInit(){
	if(typeof(curtain) == 'object'){
		curtain.init();	
	}
	/*externalLinks();
	autoCompleteOff();
	defaultClear();
	if(typeof(BrowserDetect) == 'object'){
		BrowserDetect.init();
	}
	if(typeof(loginTimer) == 'object'){
		loginTimer.init();	
	}
	if(typeof(valForm) == 'object'){
		valForm.init();	
	}
	if(typeof(cart) == 'object'){
		cart.init('cart-headline');	
	}
	ie6Check();*/
}

//Event.observe(window, 'load', emgInit);


function bookMark(url, title){
	if(document.all?true:false){ // IE
		window.external.AddFavorite(url, title);
	}
	else{
		window.sidebar.addPanel(title, url, '')
	}
}

function ajaxFill(url, container, callback){
	$(container).innerHTML = '<div style="text-align:center"><img src="'+window.CR+'/images/library/loading.gif" /></div>';
	new Ajax.Request(url, { method: 'get', onSuccess: function(ajaxReturn) {
		if(ajaxReturn.responseText == 'died'){
			window.location = window.CR+'/action/died';
			return;
		}
		$(container).innerHTML = ajaxReturn.responseText;
		eval(callback);
	}}); 
}

/*function ie6Check() {
	if (BrowserDetect.browser == 'Explorer' && BrowserDetect.version < 7) {
		var ie6Notice = document.createElement('div');
		ie6Notice.id = 'ie6-notice';
		ie6Notice.innerHTML = '<p class="title">It seems like you are using Internet Explorer 6 or lower.</p><p>IE6 is an outdated web browser that cannot provide the rich web experience that a modern web browser is able to.  This site may not display and function correctly as a result.</p><p>You may want to upgrade to one of these newer web browsers:</p><ul class="browsers"><?
			$browsers = array('Internet Explorer 8' => 'http://www.microsoft.com/windows/downloads/ie/getitnow.mspx'
							, 'Mozilla Firefox' => 'http://www.mozilla.com/en-US/firefox/'
							, 'Google Chrome' => 'http://www.google.com/chrome'
							);
			foreach ($browsers as $browser => $url) {
				?><li><a href="<?= $url ?>" title="Download <?= $browser ?>">Download <?= $browser ?></a></li><?
			}
			?></ul><p class="hide-notice"><a href="#" onclick="document.getElementById(\'ie6-notice\').style.display = \'none\'; return false;" title="Hide this notice" rel="external">Hide this notice</a></p>';
		document.body.appendChild(ie6Notice);
	}
}
// verify the captcha
function verifyCaptcha(captchaFieldid){
	var url = window.CR + "/action/verify-captcha?area=" + captchaFieldid + "&captcha=" + $(captchaFieldid).value + "&k=" + Math.round(100000*Math.random());
	valForm.ajaxRunning[captchaFieldid] = true;
	new Ajax.Request(url, { method: 'get',  onSuccess: function(verifyCaptcha2) {
			if(verifyCaptcha2.responseText == '0'){
				var error = ' is incorrect.'; //error
			}
			else{
				 var error = false; // no errror
			}
			valForm.errorHandler($(captchaFieldid), error);
			valForm.ajaxRunning[captchaFieldid] = false;
		}
	});
}

function captchaRefresh(area) {
	$(area + '-img').src = '<?= CR ?>/ajax/show-captcha?area=' + area + '&'+Math.random();
}

function showFlash(src, w, h, container){
	var s1 = new SWFObject(src,"mediaplayer",w, h,"7");
	s1.addParam("wmode", "transparent");
	if(!s1.write(container)){
		$(container).innerHTML = '<a href="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash">Click here to get the flash player.</a>';
	}
}*/
