/** OFFERS CONFIRMATION BOX **/
function doConfirm(txt)
{
var agree=confirm(txt);
if (agree)
	return true;
else
	return false;
}



/** CHECK IMAGE EXTENSION  -- THESE ARE ALSO DECLARED IN _init.php ***/
var valid_img_extensions = /(.jpg|.jpeg|.gif|.png)$/i;  
function checkImageExtension(fld){  
	if(fld.value) {  

		if (valid_img_extensions.test(fld.value))
             		return true;  
		else{
			alert("Invalid image file extension.\nCheck your config file for allowed image extensions.");
			fld.value='';
			return false;
		}

	}
	else
		return true;
 }  

/** CHECK FILE EXTENSION -- THESE ARE ALSO DECLARED IN _init.php ***/
var valid_file_extensions = /(.zip|.rar|.pdf|.jpg|.jpeg|.gif|.png)$/i;  
function checkFileExtension(fld){  
	if(fld.value) {  

		if (valid_file_extensions.test(fld.value))
             		return true;  
		else{
			alert("Invalid file extension.\nCheck your config file for allowed extensions.");
			fld.value='';
			return false;
		}
	}
	else{
		//alert('no value... field ='+fld);
		return true;
	}
 }  


/** APPEND CSS INTO HEAD TO KEEP STANDARDS COMPLIANT */
function addCss(cssCode) {
	var styleElement = document.createElement("style");
  	styleElement.type = "text/css";
  	if (styleElement.styleSheet)
    		styleElement.styleSheet.cssText = cssCode;
  	else
    		styleElement.appendChild(document.createTextNode(cssCode));

  	document.getElementsByTagName("head")[0].appendChild(styleElement);
}


/** SET FORM ACTION AND SUBMIT FORM ON ENTER, EVEN WITHOUT SUBMIT BUTTON */
function setActionAndSubmit(formId,actionBase,actionExt) {
	
	var form = document.getElementById(formId);
	
	if (window.event && window.event.keyCode == 13){

		form.action=actionBase + '/' + actionExt;
		alert(form.action);
		form.submit();
	}
	else
		alert('keycode is ' + event.keyCode);
}

/** PREVENT ENTER KEY FROM SUBMITTING FORM */
function stopRKey(evt) {
	var evt  = (evt) ? evt : ((event) ? event : null);
	var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
	if ((evt.keyCode == 13) && (node.type=="text")) { return false; }
}

/** POST FORM TO NEW WINDOW */
function formTargetBlank(formID){
	if(document.forms[formID].getAttribute("action").substring(0, 4) == "http") 
      		document.forms[formID].target = "_blank"; 
}

/** STATE AND COUNTRY RELATIONSHIP SELECTOR */
function checkState(thisForm,thisElement){


	if(document.getElementById(thisElement).value != "XX"){
	
		if(document.getElementById(thisElement).value == "AB" || document.getElementById(thisElement).value == "PQ" || document.getElementById(thisElement).value == "NT" || document.getElementById(thisElement).value == "BC" || document.getElementById(thisElement).value == "ON" || document.getElementById(thisElement).value == "SK" || document.getElementById(thisElement).value == "MB" || document.getElementById(thisElement).value == "YK" || document.getElementById(thisElement).value == "NF" || document.getElementById(thisElement).value == "NB" || document.getElementById(thisElement).value == "NS" || document.getElementById(thisElement).value == "PE" || document.getElementById(thisElement).value == "NU")
			setCountry(thisForm,'country',"CA");
		else
			setCountry(thisForm,'country',"US");
	}
	else
		setCountry(thisForm,thisElement,"0");
}

function checkCountry(thisForm,thisElement){

	if(document.getElementById(thisElement).value == "US")
		setState(thisForm,'state',"0");
	else if(document.getElementById(thisElement).value == "CA")
		setState(thisForm,'state',"00");
	else
		setState(thisForm,'state',"XX");
}

function setCountry(thisForm,thisElement,val){

	var j;
	for (j=0; j<document.getElementById(thisElement).options.length; j++){
		
		if(document.getElementById(thisElement).options[j].value == val)
			document.getElementById(thisElement).options[j].selected = true;
	}
}

function setState(thisForm,thisElement,val){

	var j;
	for (j=0; j<document.getElementById(thisElement).options.length; j++){
		
		if(document.getElementById(thisElement).options[j].value == val)
			document.getElementById(thisElement).options[j].selected = true;
	}
}

/** STUDENT FORM FUNCTIONS */
var onHolder;
onHolder = false;
function defaultToAccountHolder(){
if(!onHolder){
	document.getElementById('s_f_name').value=document.getElementById('acct_f_name').value;
	document.getElementById('s_l_name').value=document.getElementById('acct_l_name').value;
	document.getElementById('s_mid_init').value=document.getElementById('acct_mid_init').value;
	document.getElementById('s_add_1').value=document.getElementById('acct_add_1').value;
	document.getElementById('s_add_2').value=document.getElementById('acct_add_2').value;
	document.getElementById('s_city').value=document.getElementById('acct_city').value;
	document.getElementById('s_zip').value=document.getElementById('acct_zip').value;
	document.getElementById('s_email').value=document.getElementById('acct_email').value;
	document.getElementById('s_confirm_email').value=document.getElementById('acct_email').value;
	document.getElementById('s_phone_1').value=document.getElementById('acct_phone_1').value;
	document.getElementById('s_phone_2').value=document.getElementById('acct_phone_2').value;
	document.getElementById('s_phone_3').value=document.getElementById('acct_phone_3').value;
	selectState(document.getElementById('acct_state').value);
	selectCountry(document.getElementById('acct_country').value);

	onHolder = true;
} else {
	document.getElementById('s_f_name').value='';
	document.getElementById('s_l_name').value='';
	document.getElementById('s_mid_init').value='';
	selectStudentGender('M');
	document.getElementById('s_add_1').value='';
	document.getElementById('s_add_2').value='';
	document.getElementById('s_city').value='';
	document.getElementById('s_phone_1').value='';
	document.getElementById('s_phone_2').value='';
	document.getElementById('s_phone_3').value='';
	document.getElementById('s_email').value='';
	document.getElementById('s_confirm_email').value='';
	selectState('0');
	selectCountry('US');
	document.getElementById('s_zip').value='';
	onHolder = false;
	}
 }

var onAddress;
onAddress = false;
function defaultToAccountAddress(){
if(!onAddress){
	document.getElementById('s_add_1').value=document.getElementById('acct_add_1').value;
	document.getElementById('s_add_2').value=document.getElementById('acct_add_2').value;
	document.getElementById('s_city').value=document.getElementById('acct_city').value;
	document.getElementById('s_zip').value=document.getElementById('acct_zip').value;
	document.getElementById('s_phone_1').value=document.getElementById('acct_phone_1').value;
	document.getElementById('s_phone_2').value=document.getElementById('acct_phone_2').value;
	document.getElementById('s_phone_3').value=document.getElementById('acct_phone_3').value;
	selectState(document.getElementById('acct_state').value);
	selectCountry(document.getElementById('acct_country').value);

	onAddress = true;
} else {
	document.getElementById('s_add_1').value='';
	document.getElementById('s_add_2').value='';
	document.getElementById('s_city').value='';
	document.getElementById('s_phone_1').value='';
	document.getElementById('s_phone_2').value='';
	document.getElementById('s_phone_3').value='';
	selectState('0');
	selectCountry('US');
	document.getElementById('s_zip').value='';
	onAddress = false;
	}
 }

function selectStudentGender(val){
	var j;
	for (j=0; j<document.getElementById('s_gender').options.length; j++) {
		if(document.getElementById('s_gender').options[j].value == val){
			document.getElementById('s_gender').options[j].selected = true;
		}
	}
}

function selectState(val){
	var j;
	for (j=0; j<document.getElementById('state').options.length; j++) {
		if(document.getElementById('state').options[j].value == val){
			document.getElementById('state').options[j].selected = true;
		}
	}
}

function selectCountry(val){
	var j;
	for (j=0; j<document.getElementById('country').options.length; j++) {
		if(document.getElementById('country').options[j].value == val){
			document.getElementById('country').options[j].selected = true;
		}
	}
}

/*******************************************************/

/** BILLING METHOD DIV HIDER */
function checkBillingMthd(thisForm,thisElement){

	if(document.getElementById(thisElement).value == 1)
		show_div('card_info');
	else
		hide_div('card_info');
}

/** CHECKBOX FOR USING CREDIT CARD ON FILE */
function clearCardOnFile(){
	
	if(document.getElementById('use_card_on_file'))
		document.getElementById('use_card_on_file').checked = false;
}

function hideCardFields(){

	if(document.getElementById('use_card_on_file').checked == true)
		hide_div('card_fields');
	else
		show_div('card_fields');

}

/** POPULATE CREDIT CARD HOLDER FIELD WITH ACCOUNT HOLDER NAME */
function populateAcctCardName(){
	
	document.getElementById('acc_name').value = document.getElementById('acct_f_name').value + ' ' + document.getElementById('acct_mid_init').value + ' ' + document.getElementById('acct_l_name').value;
}	

/** PAYMENT TYPE DIV HIDER */
function checkPmtMthd(thisForm,thisElement){

	if(document.getElementById(thisElement).value == "CARD"){
	
		show_div('card_info');
		hide_div('check_info'); 
		hide_div('cash_info'); 
	}
    	if(document.getElementById(thisElement).value == "CHECK"){
        
		hide_div('card_info');
		show_div('check_info');
		hide_div('cash_info'); 
	}
    	if(document.getElementById(thisElement).value == "CASH"){
        
		hide_div('card_info');
		hide_div('check_info'); 
		show_div('cash_info'); 
	}

}

/** POPUP FOR CONVERTERS */
function converterPopUp(url){
	open(url,"","height=220,width=320,scrollbars=0; window.focus()");
}


/** ENCRYPT EMAIL ADDRESSES FOR SPAM CONTROL */
function CryptMailto(e){
	var n = 0;
	var r = "";
	var s = "mailto:"+e;

	e = e.replace( /@/, " [at] ");
	e = e.replace( /\./g, " [dot] ");

	for( var i=0; i < s.length; i++ ){
		n = s.charCodeAt( i );
		if( n >= 8364 )
			n = 128;

		r += String.fromCharCode(n+1);
	}
	
	document.write("<a href=\"javascript:linkTo_UnCryptMailto('"+ r +"');\">"+ e +"</a>");
}

function UnCryptMailto( s ){
	var n = 0;
	var r = "";
	
	for( var i = 0; i < s.length; i++){
    		n = s.charCodeAt( i );
    		if( n >= 8364 )
			n = 128;
    
    		r += String.fromCharCode( n - 1 );
	}
	
	return r;
}

function linkTo_UnCryptMailto( s ){
	location.href=UnCryptMailto( s );
}

/** POPULATES EVENT ADDRESS FORM */
function use_saved_event_address(address,country_code){

	document.getElementById('address').value=address;
	document.getElementById('country_code').value=country_code;
}

/** HIDES DIV */
function hide_div(div_id){
	//document.getElementById(div_id).style.visibility='hidden';
	document.getElementById(div_id).style.display='none';
}

/** SHOW DIV */
function show_div(div_id){
	//document.getElementById(div_id).style.visibility='visible';
	document.getElementById(div_id).style.display='block';
}

/** ON USER.ADD-VIDEO.PHP, THIS DETERMINES WHICH SELECT MENU APPEARS, SINGLE OR MULTI */
function user_video_trick_select(thisForm){
	
	// SINGLE TRICK
	if(thisForm.uv_uvc_id.value == 1){
    
		show_div('trick_select_container');
		show_div('uv_featured_trick_container');
		hide_div('uv_featured_tricks_container'); 
		hide_div('successful_trick_msg'); 
	}
	// COMBO OR TUTORIAL
	if(thisForm.uv_uvc_id.value == 2 || thisForm.uv_uvc_id.value == 8){
    
		show_div('trick_select_container');
		hide_div('uv_featured_trick_container');
		show_div('uv_featured_tricks_container'); 
		hide_div('successful_trick_msg'); 
	}
	// FORM, SAMPLER, SHOWREEL, OR CRASH COMBO
	if(thisForm.uv_uvc_id.value == 3 || thisForm.uv_uvc_id.value == 4 || thisForm.uv_uvc_id.value == 5 || thisForm.uv_uvc_id.value == 7){
	
		show_div('trick_select_container');
		hide_div('uv_featured_trick_container');
		show_div('uv_featured_tricks_container'); 
		show_div('successful_trick_msg'); 
	}
	// CRASH TRICK
	if(thisForm.uv_uvc_id.value == 6){
	
		show_div('trick_select_container');
		hide_div('uv_featured_trick_container');
		hide_div('uv_featured_tricks_container'); 
		hide_div('successful_trick_msg'); 
	}
	
}

function selectAll(field,_v){

	for(var i=0;i<document.getElementById(field).length;i++)
		document.getElementById(field)[i].selected=_v;

}

function checkAll(field,_v){
	for(var i=0;i<document.getElementsByName(field).length;i++)
		document.getElementsByName(field)[i].checked=_v;

}

function toggleDiv(box,div) {
	if (box.checked)
		show_div(div);
	else
  		hide_div(div);
}

function setOpacity(opacity) {

	if (navigator.appName.indexOf("Netscape")!=-1 &&parseInt(navigator.appVersion)>=5)
    		document.getElementById('maincontent').style.MozOpacity=opacity/100
 	else
    		document.getElementById('maincontent').style.filter = 'alpha(opacity=' + value + ')';

		//document.getElementById('maincontent').style.opacity = value;
		//document.getElementById('maincontent').style.filter = 'alpha(opacity=' + value + ')';
}


//enforce max characters on field
function ismaxlength(obj){

	var mlength=obj.getAttribute ? parseInt(obj.getAttribute("maxlength")) : ""
	if (obj.getAttribute && obj.value.length>mlength)
	obj.value=obj.value.substring(0,mlength)
}


function letternumber(e)
{
var key;
var keychar;

if (window.event)
   key = window.event.keyCode;
else if (e)
   key = e.which;
else
   return true;
keychar = String.fromCharCode(key);
keychar = keychar.toLowerCase();

// control keys
if ((key==null) || (key==0) || (key==8) || 
    (key==9) || (key==13) || (key==27) )
   return true;

// alphas and numbers
else if ((("abcdefghijklmnopqrstuvwxyz0123456789").indexOf(keychar) > -1))
   return true;
else
   return false;
}

function numbersonly(e)
{
var key;
var keychar;

if (window.event)
   key = window.event.keyCode;
else if (e)
   key = e.which;
else
   return true;
keychar = String.fromCharCode(key);
keychar = keychar.toLowerCase();

// control keys
if ((key==null) || (key==0) || (key==8) || 
    (key==9) || (key==13) || (key==27) )
   return true;

// numbers only
else if ((("0123456789-.").indexOf(keychar) > -1))
   return true;
else
   return false;
}

function nokeys(e)
{
var key;
var keychar;

if (window.event)
   key = window.event.keyCode;
else if (e)
   key = e.which;
else
   return true;
keychar = String.fromCharCode(key);
keychar = keychar.toLowerCase();

// control keys
if ((key==null) || (key==0) || (key==8) || 
    (key==9) || (key==13) || (key==27) )
   return true;

// alphas and numbers
else if ((("x").indexOf(keychar) > -1))
   return true;
else
   return false;
}

function popUp(url) { open(url,"","height=450,width=450,scrollbars=1; window.focus()"); }
function converterPopUp(url) { open(url,"","height=220,width=320,scrollbars=0; window.focus()"); }

function openWin( windowURL, windowName, windowFeatures )
{
return window.open( windowURL, windowName, windowFeatures ) ;
}


function alertSize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement &&
      ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
 // window.alert( 'Width = ' + myWidth );
 // window.alert( 'Height = ' + myHeight );
 return myHeight;
}


function showColor(val) {
document.colorform.hexval.value = val;
document.all["colorbox"].style.backgroundColor = val;
}

function grabColor(field) {
field.value = document.colorform.hexval.value;
field.style.backgroundColor = field.value;
}

/*************** MARQUEE FIX HERE ***********************/

var oMarquees = [], oMrunning,
	oMInterv =        20,     //interval between increments
	oMStep =          1,      //number of pixels to move between increments
	oStopMAfter =     30,     //how many seconds should marquees run (0 for no limit)
	oResetMWhenStop = false,  //set to true to allow linewrapping when stopping
	oMDirection =     'left'; //'left' for LTR text, 'right' for RTL text

/***     Do not edit anything after here     ***/

function doMStop() {
	clearInterval(oMrunning);
	for( var i = 0; i < oMarquees.length; i++ ) {
		oDiv = oMarquees[i];
		oDiv.mchild.style[oMDirection] = '0px';
		if( oResetMWhenStop ) {
			oDiv.mchild.style.cssText = oDiv.mchild.style.cssText.replace(/;white-space:nowrap;/g,'');
			oDiv.mchild.style.whiteSpace = '';
			oDiv.style.height = '';
			oDiv.style.overflow = '';
			oDiv.style.position = '';
			oDiv.mchild.style.position = '';
			oDiv.mchild.style.top = '';
		}
	}
	oMarquees = [];
}
function doDMarquee() {
	if( oMarquees.length || !document.getElementsByTagName ) { return; }
	var oDivs = document.getElementsByTagName('div');
	for( var i = 0, oDiv; i < oDivs.length; i++ ) {
		oDiv = oDivs[i];
		if( oDiv.className && oDiv.className.match(/\bdmarquee\b/) ) {
			if( !( oDiv = oDiv.getElementsByTagName('div')[0] ) ) { continue; }
			if( !( oDiv.mchild = oDiv.getElementsByTagName('div')[0] ) ) { continue; }
			oDiv.mchild.style.cssText += ';white-space:nowrap;';
			oDiv.mchild.style.whiteSpace = 'nowrap';
			oDiv.style.height = oDiv.offsetHeight + 'px';
			oDiv.style.overflow = 'hidden';
			oDiv.style.position = 'relative';
			oDiv.mchild.style.position = 'absolute';
			oDiv.mchild.style.top = '0px';
			oDiv.mchild.style[oMDirection] = oDiv.offsetWidth + 'px';
			oMarquees[oMarquees.length] = oDiv;
			i += 2;
		}
	}
	oMrunning = setInterval('aniMarquee()',oMInterv);
	if( oStopMAfter ) { setTimeout('doMStop()',oStopMAfter*1000); }
}
function aniMarquee() {
	var oDiv, oPos;
	for( var i = 0; i < oMarquees.length; i++ ) {
		oDiv = oMarquees[i].mchild;
		oPos = parseInt(oDiv.style[oMDirection]);
		if( oPos <= -1 * oDiv.offsetWidth ) {
			oDiv.style[oMDirection] = oMarquees[i].offsetWidth + 'px';
		} else {
			oDiv.style[oMDirection] = ( oPos - oMStep ) + 'px';
		}
	}
}
if( window.addEventListener ) {
	window.addEventListener('load',doDMarquee,false);
} else if( document.addEventListener ) {
	document.addEventListener('load',doDMarquee,false);
} else if( window.attachEvent ) {
	window.attachEvent('onload',doDMarquee);
}




/* COOKIE FUNCTIONS */
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}
