/*======================================================================*\
|| #################################################################### ||
|| # camweb v1.0 
|| # ---------------------------------------------------------------- # ||
|| # Copyright ©2006 CZONE Technologies. All Rights Reserved.           ||
|| # This file may not be redistributed in whole or             part. # ||
|| # ---------------- CAMWEB IS NOT FREE SOFTWARE ----------------    # ||
|| # http://www.czonetechnologies.com                                 # ||
|| #################################################################### ||
\*======================================================================*/
// JavaScript Document
//Count characters in a textfield/textarea
function count(obj,len,minval,maxval){
	var notice='';
	if(maxval && Number(obj.value) > maxval){  
	obj.value=maxval;
	notice=" Must be < "+maxval;
	}	
	if(minval && Number(obj.value) < minval){  
	//obj.value=minval;
	notice+=" Must be > "+minval;
	}	
if(len){notice+=" "+(len-obj.value.length) +" chars left.";}
	if(len && len-obj.value.length<=0){  obj.value=obj.value.substring(0,len);  }
document.getElementById('err_'+obj.name).className='showerror';
document.getElementById('err_'+obj.name).innerHTML=notice;
}
function maxval(obj,val){
document.getElementById('err_'+obj.name).className='showerror';


}
//Dynamically add/remove div elements to/from the end of an existing element.Like pop/push to an array...
function dynamic(what,divid,html){
nodes=document.getElementById(divid).getElementsByTagName('div');
if(what=='add'){
var dd=document.createElement('div');
dd.setAttribute('id',nodes.length+1);
count++;
dd.innerHTML=html;
document.getElementById(divid).appendChild(dd);
 }else{
 dd=nodes[nodes.length-1];
 document.getElementById(divid).removeChild(dd);
 }
}

var customInvalid=false;
///validation functions
function validate(frm){

//	frm=document.forms[0];
var err=frm.getElementsByTagName('span');
var corr=true;
if(customInvalid){corr=false;}

for(i=0;i<frm.elements.length;i++){
var e=frm.elements[i];
//If this element is visibly hidden, lets leave the check and move ahead.
if(e.parentNode.parentNode.className=='hide'){ continue;}
if(e.name.indexOf("[]")!=-1){var ename=e.name.substr(0,e.name.length-2);}else{var ename=e.name;}
if(err['err_'+ename]){
	if(err['err_'+ename].innerHTML){
if(ename.indexOf('username')!=-1){
	
	
	//alert(digi_err+":"+char_err+":"+corr);
}else if(ename.indexOf('password')!=-1){
	
}else if(ename=='cpassword'){
	if(e.value!=frm.elements['password'].value){
	//alert(err['err_'+ename]);
	err['err_'+ename].className='showerror';
	corr=false;
	}else{
	err['err_'+ename].className='error';
	}
}else if(ename.indexOf('email')!=-1){
	
}else if(ename=='agree'){
	if(e.checked==false){
	//alert(err['err_'+ename]);
	err['err_'+ename].className='showerror';
	corr=false;
	}else{
	err['err_'+ename].className='error';
	}
}else if(e.type=='select-one'){
	if(e.value=='' || e.value=='select' || e.value=='Select'){
	//alert(err['err_'+ename]);
	err['err_'+ename].className='showerror';
	corr=false;
	}else{
	err['err_'+ename].className='error';
	}
}else{
	if(e.value=='' && e.type!='checkbox'){
		if(ename=='sponsor'){ continue;}
	//alert(ename+":"+err['err_'+ename]);
	err['err_'+ename].className='showerror';
	corr=false;
	}else{
	err['err_'+ename].className='error';
	}
}
	}
}
}
if(!corr){
return false;
}else{
	//frm.submit();
return true;
}
}
function showHide(elementID) {
    var desc = null;

    if (document.getElementById) {
      desc = document.getElementById("cnt_desc_" + elementID);
    } else if (document.all) {
      desc = document.all["cnt_desc_" + elementID];
    } else if (document.layers) {
      desc = document.layers["cnt_desc_" + elementID];
    }

    if (desc) {
      if (desc.style.display == 'none') {
        expand(elementID);
      } else {
        collapse(elementID);
      }
    }
  }

  function expand(elementID) {
    var cnt = null;
    var desc = null;
    var icon = null;

    if (document.getElementById) {
      cnt = document.getElementById("cnt_" + elementID);
      desc = document.getElementById("cnt_desc_" + elementID);
      icon = document.getElementById("cnt_icon_" + elementID);
    } else if (document.all) {
      cnt = document.all["cnt_" + elementID];
      desc = document.all["cnt_desc_" + elementID];
      icon = document.all["cnt_icon_" + elementID];
    } else if (document.layers) {
      cnt = document.layers["cnt_" + elementID];
      desc = document.layers["cnt_desc_" + elementID];
      icon = document.layers["cnt_icon_" + elementID];
    }

    if (desc.style.display == 'none') {
		cnt.style.fontWeight='';
		cnt.style.textAlign='left';
       cnt.style.backgroundColor = '#eeeeee';
      cnt.style.border = '.1em solid #cccccc';
      cnt.style.padding = '5px';
      cnt.style.marginBottom = '5px';
	  cnt.style.marginLeft = '20px';
      desc.style.display = 'block';
      if(icon){icon.src = "images/icon_minus.gif";}
    }
  }

  function collapse(elementID) {
    var cnt = null;
    var desc = null;
    var icon = null;

    if (document.getElementById) {
      cnt = document.getElementById("cnt_" + elementID);
      desc = document.getElementById("cnt_desc_" + elementID);
      icon = document.getElementById("cnt_icon_" + elementID);
    } else if (document.all) {
      cnt = document.all["cnt_" + elementID];
      desc = document.all["cnt_desc_" + elementID];
      icon = document.all["cnt_icon_" + elementID];
    } else if (document.layers) {
      cnt = document.layers["cnt_" + elementID];
      desc = document.layers["cnt_desc_" + elementID];
      icon = document.layers["cnt_icon_" + elementID];
    }

    if (desc.style.display != 'none') {
	  cnt.style.textAlign='left';
	  cnt.style.fontWeight='bold';
      cnt.style.backgroundColor = '';
      cnt.style.border = '';
      cnt.style.padding = '';
      cnt.style.marginBottom = '2px';
	  cnt.style.marginLeft = '20px';
      desc.style.display = 'none';
      if(icon){icon.src = "images/icon_plus.gif";}
    }
  }

  function expandAll() {
    var cnt = null;

    if (document.body.getElementsByTagName) {
      cnt = document.body.getElementsByTagName('DIV');
    } else if (document.body.all) {
      cnt = document.body.all.tags('DIV');
    }

    if (cnt) {
      for (var i=0; i<cnt.length; i++) {
        if (cnt[i].id.substring(0, 4) == 'cnt_') {
          if (cnt[i].id.substring(0, 5) != 'cnt_d') {
            expand(cnt[i].id.substring(4));
          }
        }
      }
    }
  }

  function collapseAll() {
    var cnt = null;

    if (document.body.getElementsByTagName) {
      cnt = document.body.getElementsByTagName('DIV');
    } else if (document.body.all) {
      cnt = document.body.all.tags('DIV');
    }

    if (cnt) {
      for (var i=0; i<cnt.length; i++) {
        if (cnt[i].id.substring(0, 4) == 'cnt_') {
          if (cnt[i].id.substring(0, 5) != 'cnt_d') {
            collapse(cnt[i].id.substring(4));
          }
        }
      }
    }
  }
  //ajax function
   function makeRequest(url,num) {
        http_request = false;
        if (window.XMLHttpRequest) { // Mozilla, Safari,...
            http_request = new XMLHttpRequest();
            if (http_request.overrideMimeType) {
                http_request.overrideMimeType('text/xml');
                // See note below about this line
            }
        } else if (window.ActiveXObject) { // IE
            try {
                http_request = new ActiveXObject("Msxml2.XMLHTTP");
            } catch (e) {
                try {
                    http_request = new ActiveXObject("Microsoft.XMLHTTP");
                } catch (e) {}
            }
        }

        if (!http_request) {
            alert('Giving up :( Cannot create an XMLHTTP instance');
            return false;
        }
		
        http_request.onreadystatechange = eval("alertContents"+[num]);
        http_request.open('GET', url, true);
        http_request.send(null);
		//alert('hi');

    }
	///
function alertContents() {
        if (http_request.readyState == 4) {
            if (http_request.status == 200) {
           var xmldoc = http_request.responseText;
		   return xmldoc;
			 } else {
                return 'There was a problem with the request.';
            }
        }else{
		//document.getElementById('Layer1').style.visibility="visible";
		}

    }

	// Image resize function
function makeImg($src,$height,$width){
var $hmax=100;
var $wmax=100;
  $hscale = $height / $hmax;
  $wscale = $width / $wmax;
if (($hscale > 1) || ($wscale > 1)) {
$scale = ($hscale > $wscale)?$hscale:$wscale;
} else {
$scale = 1;
}
$newwidth = Math.floor($width / $scale);
$newheight= Math.floor($height / $scale);
//alert($newwidth+":"+$newheight);
document.write('<img src='+$src+' width='+$newwidth+' height='+$newheight+' >');
}

//Show hide options...
function show_hide_options(refdiv, sign){

	var domItem=document.getElementById(refdiv);

	if(domItem.style.display=='none'){
		for(var i=1; i<=refdiv.substr(1); i++){
			tmp_id=refdiv.substr(0,1)+i;
			document.getElementById(tmp_id).className='showerror';
		}
	if(sign){
		sign.innerHTML=sign.innerHTML.replace('More','Fewer');
		sign.innerHTML=sign.innerHTML.replace('down','up');
		}
	}else{
		for(var i=1; i<=refdiv.substr(1); i++){
			tmp_id=refdiv.substr(0,1)+i;
			document.getElementById(tmp_id).style.display='none';
		}
	if(sign){
		sign.innerHTML=sign.innerHTML.replace('Fewer','More');
		sign.innerHTML=sign.innerHTML.replace('up','down');
		}
	}
}
function show_hide(divid){
if(document.getElementById(divid).style.display=="none"){
	document.getElementById(divid).style.display="";	
}else{
	document.getElementById(divid).style.display="none";
}
}
// Create a div element dynamically, put it inside an element 'e' and also put in some HTML in it..
function create_element(e,html){
var test=document.createElement('div');
document.getElementById(e).appendChild(test);
test.innerHTML=html;
}
function showmydiv(showid,hideid){
	hide=hideid.split(",");
	for(i=0;i<hide.length;i++){
		document.getElementById(hide[i]).style.display='none';
	}
	document.getElementById(showid).style.display='';
}
//AJAX
function ajax_form(div,url,form,post){

if(validate(form)==false){return false;}
if(post==''){post='post';}
post = post.toLowerCase();
//alert(post);
var query=Form.serialize(form);
// Add the value of the chosen 'button'
//if(ele){if(ele.type=='button'){query+= "&"+ele.name+"="+ele.value;}}
var divs=form.getElementsByTagName('div');
//alert(divs['indicator']);
var indicator=divs['indicator'];
//alert(query);
new Ajax.Updater(div,url,{method:post,parameters:query,evalScripts:true,asynchronous:true,onLoading:function(){Element.show(indicator);},onComplete:function(){Element.hide(indicator);}});
return false;
}

function ajax_submit(div,url,query,post){
	if(post==''){post='get';}
	post = post.toLowerCase();
	new Ajax.Updater(div,url,{method:post,parameters:query,evalScripts:true,asynchronous:true});
	return false;
}

function switchTabs(active){
	var inact = active.parentNode.parentNode.childNodes;
//var inact=inactive.split(",");
for(i=0;i<inact.length;i++){
	//alert(inact[i].childNodes[0].nodeName);
$(inact[i]).childNodes[0].className='';
}
$(active).className='active';
}

//Palette functions
function showPalette(obj){
	var offset = Position.cumulativeOffset(obj);
  currentField = obj;
  $('colorpalette').style.left = offset[0] + 'px';
  $('colorpalette').style.top = offset[1] + obj.offsetHeight + 'px';
	$('colorpalette').style.display = '' == $('colorpalette').style.display ? 'none' : '';
}
function setColor(color) {
	currentField.value = color;
  currentField.style.backgroundColor = color;
  $('colorpalette').style.display = 'none';
}

// function for grids
function gridchk(frm){
var e=frm.elements;	c=new Array();
for(i=1;i<e.length;i++){
	if(e[i].type=='checkbox'){ c.push(e[i]);}
}
	for(i=0;i<c.length;i++){
	//alert(i+":"+c[i].checked);
	if(c[0].checked==true){
     c[i].checked=true;
	 c[i].parentNode.parentNode.className='active';	
	}
	if(c[0].checked==false){
	 c[i].parentNode.parentNode.className='inactive';
     c[i].checked=false;
	}
	 
	}
	for(i=1;i<c.length;i++){
		if(c[i]){gridval(c[i]);}
	}
}
function gridclass(chk){
	gridval(chk);
//alert(chk.checked);
if(chk.checked==true){
chk.parentNode.parentNode.className='active';	
}else{
chk.parentNode.parentNode.className='inactive';		
}
}

function genlink(frm){
 var e=frm.elements;
 for(i=1; i<e.length; i++){
 
  frm.catid.value+="_"+e[i].value;
   }
   frm.submit();
}
function setlink(){
 document.forms[1].catid.value= document.forms[0].elements[document.forms[0].elements.length-1].value;
}

function timestamp(d,m,y){
dt=new Date();
if(d){dt.setDate(d);}
if(m){dt.setMonth(m-1);}
if(y){dt.setFullYear(y,m,d);}
return Math.round(dt.getTime()/1000);
}

function gridval(chk){
	var str=chk.value;
	var arr=str.split("||");
	//alert(arr);
	var e=chk.parentNode.childNodes;
	var h=new Array();var z=0;
	for(k=0;k<e.length;k++){
		//
		if(e[k].type=='hidden'){ h.push(e[k]);}
	}
	//alert(arr.length);
		for(j=0;j<arr.length;j++){
			var temp=arr[j].split("::");
			//alert(h[z]+":"+z);
			if(chk.checked){h[z].name=temp[0];h[z].value=temp[1];}else{h[z].name='';h[z].value='';}
			z++;
		}
}

//Finds the position of an element in a page.
function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}

function showBaloon(element,tipText){
	var pos=findPos(element);	
	//alert('position'+element.style.width);
//	document.getElementById("ballon").style.height='50px';
	document.getElementById("baloon").style.display='block';
	document.getElementById("baloon").innerHTML=tipText;
	document.getElementById("baloon").style.top=pos[1]-13+"px";	
	document.getElementById("baloon").style.left=pos[0]+18+"px";	
	//alert('position'+document.getElementById("baloon").style.top+":"+document.getElementById("baloon").style.left);
}

function showDeniedBaloon(element,tipText,height,bgImage){
	
	if(bgImage==2){
		document.getElementById("bulleAccessDenied").style.backgroundImage='url(images/speechBalloons/bulleAccessDenied_2.gif)';
	}else if(bgImage==3){
		document.getElementById("bulleAccessDenied").style.backgroundImage='url(images/speechBalloons/bulleAccessDenied_3.gif)';
	}else{
		document.getElementById("bulleAccessDenied").style.backgroundImage='url(images/speechBalloons/bulleAccessDenied.gif)';
	}
	var pos=findPos(element);	
	//alert('position '+element+":"+pos[1]+":"+pos[0]);
	document.getElementById("bulleAccessDenied").style.display='block';
	if(tipText){document.getElementById("bulleAccessDenied").innerHTML=tipText;}
	if(!height){height=26;}
	document.getElementById("bulleAccessDenied").style.top=pos[1]+height+"px";	
	document.getElementById("bulleAccessDenied").style.left=pos[0]-288+"px";	
	//alert('position'+document.getElementById("baloon").style.top+":"+document.getElementById("baloon").style.left);
}

function hideBaloon(){
	document.getElementById("baloon").style.display='none';
}

function characterCounter(container,shownumber)
{
	var present = container.value;
	count = present.length;
	shownumber.value = 2000-count;
	if(count >= 2000)
	{
		container.value = present.substring(0,2000);
		shownumber.value=0;
		//alert("You can not add more word");
	}
}

function callAjax(div,href,callback){
	var option={ evalScripts:true,asynchronous:true };
	if(callback){ option.onComplete=callback;}
	new Ajax.Updater(div,href,option);
}

var slideno1=0;
var slideno2=0;
function slide_set(x,image_name,no,img)
{
//	x=(x<0)?0:x;visibility = "hidden";
	if(no==1){
		var previous='previous';
		var next='next';
		slideno1=parseInt(x);
	} else {
		var previous='previous1';
		var next='next1';
		slideno2=parseInt(x);
	}
	document.getElementById(previous).style.display='inline';
	document.getElementById(next).style.display='inline';
	if(x<=0){
//	x=0;
	document.getElementById(previous).style.display='none';
	}
	if(x>(img.length-2)){
//	x=(img.length-1);
	document.getElementById(next).style.display='none';
	}
//	x=(x>(img.length-1))?(img.length-1):x;
//	alert(image_name);
	document.getElementById(image_name).src=img[x];
//	(no==1)?slideno1=parseInt(x):slideno2=parseInt(x);
//	document.getElelmentById('slide_image').src=img[x];
}

function __ajaxSelect(target,option){ 
	//var option = new Array();
	//option = req.responseText.split("\n");
	document.getElementById(target).options.length=0;
	document.getElementById(target).options[0]= new Option("sélectionnez :","");
	for(var i=0;i<option.length;i++){
		var opt= option[i].split(",");
		//alert(option[i]+":"+opt[0]+":"+opt[1]);
		document.getElementById(target).options[i+1]= new Option(opt[0],opt[1]);
	}
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}
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 MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){ document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}



function set_cookie( name, value, expires, path, domain, secure ) 
{
	// set time, it's in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );
	
	/*
	if the expires variable is set, make the correct 
	expires time, the current script below will set 
	it for x number of days, to make it for hours, 
	delete * 24, for minutes, delete * 60 * 24
	*/
	if ( expires )
	{
	expires = expires * 1000 ;
	}
	var expires_date = new Date( today.getTime() + (expires) );
	
	document.cookie = name + "=" +escape( value ) +
	( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + 
	( ( path ) ? ";path=" + path : "" ) + 
	( ( domain ) ? ";domain=" + domain : "" ) +
	( ( secure ) ? ";secure" : "" );
}

// this fixes an issue with the old method, ambiguous values 
// with this test document.cookie.indexOf( name + "=" );
function get_cookie( check_name ) {
	// first we'll split this cookie up into name/value pairs
	// note: document.cookie only returns name=value, not the other components
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f
	
	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		// now we'll split apart each name=value pair
		a_temp_cookie = a_all_cookies[i].split( '=' );
		
		
		// and trim left/right whitespace while we're at it
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
	
		// if the extracted name matches passed check_name
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			// we need to handle case where cookie has no value but exists (no = sign, that is):
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			// note that in cases where cookie is initialized but no value, null is returned
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}		

// this deletes the cookie when called
function delete_cookie( name, path, domain ) {
if ( get_cookie( name ) ) document.cookie = name + "=" +
( ( path ) ? ";path=" + path : "") +
( ( domain ) ? ";domain=" + domain : "" ) +
";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

function getFlashMovieObject(movieName)
{
  if (window.document[movieName]) 
  {
      return window.document[movieName];
  }
  if (navigator.appName.indexOf("Microsoft Internet")==-1)
  {
    if (document.embeds && document.embeds[movieName])
      return document.embeds[movieName]; 
  }
  else // if (navigator.appName.indexOf("Microsoft Internet")!=-1)
  {
    return document.getElementById(movieName);
  }
}
var emailValidationString="^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*\.(.*)";