// -------------------------------------------------------------
function uploadingpopup() {

  l = getElement('lollipop');
  f = getElement('uploadform');
  f.style.visibility = 'hidden';
  f.style.display    = 'none';
  l.style.visibility = 'visible';
  l.style.display    = 'block';

}

// -------------------------------------------------------------
function showgiftrows() {
//not using gifts right now, so this is commented
/*
  checked = false;
  for ( i = 0; !checked && ( i < document.forms["input"]["purchase"].length ); i++ ) {
    if ( document.forms["input"]["purchase"][ i ].checked == true )
      checked = document.forms["input"]["purchase"][ i ].value;
  }

  g = getElement('gift');
  if ( checked == 'g' ) {
    g.style.visibility = 'visible';
    g.style.display    = 'block';
  }
  else {
    g.style.visibility = 'hidden';
    g.style.display    = 'none';
  }
*/
//if we have a member that comes to the modify user page and the initial value is either 'no purchase' or 'free limited membership', then hide credit card options...
 if (typeof document.forms["input"]["purchase"]!="undefined"){
  if (document.forms.input.purchase[0].checked){
    elements = Array( 'trpaymentinfo');

    for ( i in elements ) {

      o = getElement( elements[i] );

      if ( o ) {
        o.classname = '';
        o.style.visibility = 'hidden' ;
        o.style.display    = 'none' ;
      }

    }
  }else{
    elements = Array( 'trpaymentinfo');

    for ( i in elements ) {

      o = getElement( elements[i] );

      if ( o ) {
        o.classname = '';
        o.style.visibility = 'visible' ;
        o.style.display    = '' ;
      }

    }

  }
 }




}

// -----------------------------------------------------------stufftohide-
function sth() {
//hide credit card options if 'free limited membership' is the intial value

if (typeof document.forms["input"]!="undefined"){
/*
 if (typeof document.forms["input"]["productid"]!="undefined"){
  if (document.forms["input"]["productid"][0].checked){
    elements = Array( 'trcardtype', 'trnameoncard', 'trcardnumber', 'trexpiration', 'trcardcvs', 'contactinfo', 'traddress', 'trcity', 'trstate', 'trzip', 'trcountry', 'trpaymentinfo', 'trphone');

    for ( i in elements ) {

      o = getElement( elements[i] );

      if ( o ) {
        o.classname = '';
        o.style.visibility = 'hidden' ;
        o.style.display    = 'none' ;
      }

    }
  } 
 }
*/

//if credit card is not checked, then hide all the credit card options

 if (typeof document.forms["input"]["paymenttype"]!="undefined" ){
 if (document.forms.input.paymenttype[0] != undefined && document.forms.input.paymenttype[0] != null){
//  if (typeof document.forms.input.paymenttype!="undefined" && document.forms.input.paymenttype==!null){
  if (!document.forms.input.paymenttype[0].checked){
    elements = Array( 'trcardtype', 'trnameoncard', 'trcardnumber', 'trexpiration', 'trcardcvs', 'contactinfo');

    for ( i in elements ) {

      o = getElement( elements[i] );

      if ( o ) {
        o.classname = '';
        o.style.visibility = 'hidden' ;
        o.style.display    = 'none' ;
      }

    }
  }
  }
 }
//if we have a member that comes to the modify user page and the initial value is either 'no purchase' or 'free limited membership', then hide credit card options...
 if (typeof document.forms["input"]["purchase"]!="undefined"){
  if (document.forms.input.purchase[0].checked){ 
    elements = Array( 'trpaymentinfo');

    for ( i in elements ) {

      o = getElement( elements[i] );

      if ( o ) {
        o.classname = '';
        o.style.visibility = 'hidden' ;
        o.style.display    = 'none' ;
      }

    }
  }
 }

/*
   if (document.forms["input"]["productid"][0].checked){
     o = getElement( 'traddress' );
     o.style.visibility='hidden';
     o.style.display   ='none';
     alert("heh heh!");
   }else{
     alert("crap");
   }
*/
}
}
// -------------------------------------------------------------
function validaterenewal() {

//  if ( null == document.forms.input.paymenttype[0] )
//    return true;

  ok =
    (
      document.forms["input"]["autorenewal"].checked && 
      document.forms["input"]["paymenttype"][0].checked 
    ) ||
    !document.forms["input"]["autorenewal"].checked
  ;

  return ok;

}


// -------------------------------------------------------------
function validateccandpurchase() {

//if there is no purchase, ignore no entry in CC fields

  ok =
    !document.forms["input"]["purchase"][0].checked
  ;

  return ok;

}


// -------------------------------------------------------------
function validategiftemail( regexp ) {

  giftid = -1;

  for ( i = 0; i < document.forms.input.purchase.length; i++ ) {
    if ( document.forms.input.purchase[i].value == 'g' )
      giftid = i;
  } 

  if ( ( giftid != -1 ) && document.forms.input.purchase[giftid].checked ) {
    
    ok = 
      document.forms["input"]["giftemail"].value.match( regexp ) == 
      document.forms["input"]["giftemail"].value;
    return ok;

  }
  else
    return true;

}

// -------------------------------------------------------------
function validatecardtype() {

//  if ( 
//       ( null == document.forms.input.paymenttype[0] &&
//	!document.forms.input.purchase[0].checked ) ||
//       (document.forms.input.paymenttype[0].checked  &&
//	!document.forms.input.purchase[0].checked )  ) {

    ok = false;
    for ( i = 0; !ok && ( i < document.forms.input.cardtype.length ); i++ ) {
      ok = ok || ( document.forms.input.cardtype[ i ].checked == true );
    }

    return ok;

//  }
//  else
//    return true;

}


// -------------------------------------------------------------
function cleanvalidaterequired( element, regexp, mustmatch ) {
    ok =
      document.forms["input"][ element ].value.match( regexp ) ==
      document.forms["input"][ element ].value;

    if ( mustmatch )
      return ok;
    else
      return !ok;
}
// -------------------------------------------------------------
function freememvalidaterequired( element, regexp, mustmatch ) {
// if (
//  !document.forms.input.productid[0].checked
// ){
    ok =
      document.forms["input"][ element ].value.match( regexp ) ==
      document.forms["input"][ element ].value;

    if ( mustmatch )
      return ok;
    else
      return !ok;

  //}else return true;
//return true;
}

// -------------------------------------------------------------
function validaterequired( element, regexp, mustmatch ) {
if (typeof document.forms["input"]["paymenttype"]!="undefined" && document.forms["input"]["paymenttype"]!=null){
// if (typeof document.forms.input.paymenttype!="undefined" && document.forms.input.paymenttype==!null){

 if ( document.forms.input.paymenttype[0].checked ) { //if creditcard is selected


//       ( null == document.forms.input.paymenttype[0] &&
//	!document.forms.input.purchase[0].checked ) ||
//       ( document.forms.input.paymenttype[0].checked &&
//	!document.forms.input.purchase[0].checked )   ) 
    ok = 
      document.forms["input"][ element ].value.match( regexp ) == 
      document.forms["input"][ element ].value;

    if ( mustmatch ) 
      return ok;
    else
      return !ok;
  }
  else
    return true;
 }
}

// -------------------------------------------------------------
function showcreditcardrows( type ) {

  elements = Array( 'trcardtype', 'trnameoncard', 'trcardnumber', 'trexpiration', 'trcardcvs', 'contactinfo' );
  v        = getElement('visiblerow');

  for ( i in elements ) {

    o = getElement( elements[i] );

    if ( o ) {
      o.classname = '';
      o.style.visibility = ( type == 'creditcard' ? 'visible' : 'hidden' );
      o.style.display    = ( type == 'creditcard' ? v.style.display : 'none' );
    }

  }
    

}

// -------------------------------------------------------------
function freeuserorder( type ) {

  elements = Array(  'trnewsletter', 'trretentionemails','traddress','traddress','trcity','trstate','trzip','trcountry','trphone','trpaymentinfo' );
  v        = getElement('visiblerow');

  for ( i in elements ) {

    o = getElement( elements[i] );

    if ( o ) {
      o.classname = '';
      o.style.visibility = ( type == '3' ? 'hidden' : 'visible' );
      o.style.display    = ( type == '3' ? 'none' : v.style.display );
    }

  }


}

// -------------------------------------------------------------
function getElement( id, indoc ) {

  if ( undefined == indoc ) 
    indoc = document;

  obj = null;

  if ( indoc.getElementById ) {
    obj = indoc.getElementById( id );
  }
  else
    if ( indoc.all ) 
      obj = indoc[ id ];

  return obj;

}

// ----------------------------------------------------------------------------
function selectall( name ) {

  e = getElement( name );

  for ( i = 0; i < e.length; i++ )
    e[i].selected = true;

}

// ----------------------------------------------------------------------------
function checkall( name, value ) {

  e = document.getElementsByTagName( 'input' );

  for ( i = 0; i < e.length; i++ ) {
    if ( e[i].name.substring( 0, name.length ) == name )
      e[i].checked = value;
  }

}

// ----------------------------------------------------------------------------
function addrecipient() {

  e      = document.getElementsByTagName( 'input' );
  emails = opener.document.forms.input.emails.value;

  for ( i = 0; i < e.length; i++ ) {
    
    if ( 
         e[i].checked &&
         ( 
           ( e[i].name.substring( 0, 10 ) == 'contactcbx' )
           ||
           ( e[i].name.substring( 0, 6 ) == 'grpcbx' )
         )
       )
    if ( emails.length ) 
      emails += '\n' + e[i].value;
    else
      emails = e[i].value;

  }

  opener.document.forms.input.emails.value = emails;

}
// ----------------------------------------------------------------------------
//this function gives all browsers the abilty to copy text to the end user clipboard
function copy(text2copy) {
  if (window.clipboardData) {
    window.clipboardData.setData("Text",text2copy);
  } else {
    var flashcopier = 'flashcopier';
    if(!document.getElementById(flashcopier)) {
      var divholder = document.createElement('div');
      divholder.id = flashcopier;
      document.body.appendChild(divholder);
    }
    document.getElementById(flashcopier).innerHTML = '';
    var divinfo = '<embed src="_clipboard.swf" FlashVars="clipboard='+escape(text2copy)+'" width="0" height="0" type="application/x-shockwave-flash"></embed>';
    document.getElementById(flashcopier).innerHTML = divinfo;
  }
}



//----------------------------------------------------------------
//this function hides the flash interface and shows the processed anim
function hideFlash(myUrl, color, width, height, animid) {
        var ele = document.getElementById('flashDiv');
        ele.style.display = 'none';
        var myContent;
        var h=0;
        var s=0;
        var v=0;
        //find a color that will have a lot of contrast based on the background color - so the text can be read easily
        a=rgbToHsv(color);//first convert to hsv
        a[0] = a[0] * 360; //convert to degrees
        a[0] = (a[0] + 180) % 360;//now flip 180 degrees
        a[0] = a[0] / 360;
        a[1] = 1.0 - a[1];//invert saturation and value
        a[2] = 1.0 - a[2];
        fontcolor=hsvToRgb(a[0], a[1], a[2]);//convert back to rgb

        if (myUrl.match(/\.gif/)) {
          myContent = '<a href="sendcontent.php?contentid=' + animid + '&fullpath=' + myUrl + '"><img src="' + myUrl + '"><br><font color="' + fontcolor + '">Click Here to Download<br>Background Color: '+color+'</font></a>';
        }
        //width=width-1;
        //height=height-1;
        if (myUrl.match(/\.swf/)) {
          myContent = '<embed src="' + myUrl + '" width="' + width + '" height="' + height + '" type="application/x-shockwave-flash"></embed>';
          myContent = myContent + '<br><a href="sendcontent.php?contentid=' + animid + '&fullpath=' + myUrl + '"><font color="' + fontcolor + '">Click Here to Download';
//          myContent = myContent + ' width:'+width+' height:'+height;
//          myContent = myContent + ' h:'+a[0]+' s:'+a[1]+' v:'+a[2];
          if ( color == 999999 ) {
            myContent = myContent + '</font></a>';
          }else{
            myContent = myContent + '<br>Background Color: '+color+'</font></a>';
          }
        }
        document.getElementById('myAnim').innerHTML = myContent;
        //document.getElementById('myAnim').innerHTML = color;
        document.getElementById('myResults').style.display = 'block';
        //document.getElementById('myAnim').style.width = '800px';
        //document.getElementById('myAnim').style.position = 'absolute';
        //document.getElementById('myAnim').style.left = '50%';
        //document.getElementById('myAnim').style.top = '100px';
        //document.getElementById('myAnim').style.marginLeft = '-400px';
        if ( color == 999999 ) {
        document.getElementById('myColor').style.backgroundImage = 'url(images/checker.gif)';
        } else {
        document.getElementById('myColor').style.backgroundColor = color;
        document.getElementById('myColor').style.backgroundImage = 'none';

        }

}



//this function hides the flash interface and shows the processed anim
function hideFlashClip(myUrl, color, width, height, clipid) {
        var ele = document.getElementById('flashDiv');
        ele.style.display = 'none';
        var myContent;
        if(width >500){
          height=Math.round(500*height/width);
          width=500;
        }
        if(height>500){
          width=Math.round(500*width/height);
          height=500;
        }
        a=rgbToHsv(color);//first convert to hsv
        a[0] = a[0] * 360; //convert to degrees
        a[0] = (a[0] + 180) % 360;//now flip 180 degrees
        a[0] = a[0] / 360;
        a[1] = 1.0 - a[1];//invert saturation and value
        a[2] = 1.0 - a[2];
        fontcolor=hsvToRgb(a[0], a[1], a[2]);//convert back to rgb
//      myContent = '<a href="' + myUrl + '">Click Here to Download</a>';
        myContent = '<a href="sendcontent.php?contentid=' + clipid + '&fullpath=' + myUrl + '"><img src="' + myUrl + '" width="'+width+'" height="'+height+'"><br><font color="' + fontcolor + '">Click Here to Download';
        if ( color == 999999 ) {
          myContent = myContent + '</font></a>';
        }else{
          myContent = myContent + '<br>Background Color: '+color+'</font></a>';
        }
        document.getElementById('myAnim').innerHTML = myContent;
        //document.getElementById('myAnim').innerHTML = color;
        document.getElementById('myResults').style.display = 'block';
        if ( color == 999999 ) {
        document.getElementById('myColor').style.backgroundImage = 'url(images/checker.gif)';
        } else {
        document.getElementById('myColor').style.backgroundColor = color;
        document.getElementById('myColor').style.backgroundImage = 'none';

        }

}


	
//----------------------------------------------------------------
//this function shows the flash interface after anim is viewed

function findFlash() {
document.getElementById('myResults').style.display = 'none';
document.getElementById('flashDiv').style.display = 'block';
}


//----------------------------------------------------------------
//this function converts rgb to hsv colorspace, so we can find contrasting colors,
//so the user can read the text on any given background ... 'click to download'

function rgbToHsv(hexcolor){
    r = HexToR(hexcolor);
    g = HexToG(hexcolor);
    b = HexToB(hexcolor);
    r = r/255, g = g/255, b = b/255;
    var max = Math.max(r, g, b), min = Math.min(r, g, b);
    var h, s, v = max;
    var d = max - min;
    s = max == 0 ? 0 : d / max;

    if(max == min){
        h = 0; // achromatic
    }else{
        switch(max){
            case r: h = (g - b) / d + (g < b ? 6 : 0); break;
            case g: h = (b - r) / d + 2; break;
            case b: h = (r - g) / d + 4; break;
        }
        h /= 6;
    }
//    alert("h=" + h + "s=" + s);
    return [h, s, v];
}



/**
 * Converts an HSV color value to RGB. Conversion formula
 * adapted from http://en.wikipedia.org/wiki/HSV_color_space.
 * Assumes h, s, and v are contained in the set [0, 1] and
 * returns r, g, and b in the set [0, 255].
 *
 * @param   Number  h       The hue
 * @param   Number  s       The saturation
 * @param   Number  v       The value
 * @return  Array           The RGB representation
 */
function hsvToRgb(h, s, v){
    var r, g, b;

    var i = Math.floor(h * 6);
    var f = h * 6 - i;
    var p = v * (1 - s);
    var q = v * (1 - f * s);
    var t = v * (1 - (1 - f) * s);

    switch(i % 6){
        case 0: r = v, g = t, b = p; break;
        case 1: r = q, g = v, b = p; break;
        case 2: r = p, g = v, b = t; break;
        case 3: r = p, g = q, b = v; break;
        case 4: r = t, g = p, b = v; break;
        case 5: r = v, g = p, b = q; break;
    }
    return(RGBtoHex(r*256,g*256,b*256));
//    return [r * 255, g * 255, b * 255];
}


//misc short functions
function HexToR(h) {return parseInt((cutHex(h)).substring(0,2),16)}
function HexToG(h) {return parseInt((cutHex(h)).substring(2,4),16)}
function HexToB(h) {return parseInt((cutHex(h)).substring(4,6),16)}
function cutHex(h) {return (h.charAt(0)=="#") ? h.substring(1,7):h}
function RGBtoHex(R,G,B) {return toHex(R)+toHex(G)+toHex(B)}
function toHex(N) {
 if (N==null) return "00";
 N=parseInt(N); if (N==0 || isNaN(N)) return "00";
 N=Math.max(0,N); N=Math.min(N,255); N=Math.round(N);
 return "0123456789ABCDEF".charAt((N-N%16)/16)
      + "0123456789ABCDEF".charAt(N%16);
}

