function getget(name) {
  var q = document.location.search;
  var i = q.indexOf(name + '=');

  if (i == -1) {
    return false;
  }

  var r = q.substr(i + name.length + 1, q.length - i - name.length - 1);

  i = r.indexOf('&');

  if (i != -1) {
    r = r.substr(0, i);
  }

  return r.replace(/\+/g, ' ');
}

function getURL(module_name, component_name, tpl, get_params)
{
  var url = module_name+'/'+component_name+'/'+tpl+'/';
  
  var sess_id = getget('PHPSESSID');
  
  var get_str = '';
  
  get_str = 'index.php?params=';
  url = get_str + url ;
  
  if(sess_id != '')
  {
    sess_str = 'PHPSESSID='+sess_id;
    url = url + '&' + sess_str;
  }
  
  if(get_params != '')
	  url = url + '&' + get_params;
  
	return url;
}

function openVideo(filename)
{	

  var url = website_url + getURL('training-course', 'video', 'open', 'filename=' + filename);
  	
  window.open( url, 'Video', 'width=785,height=640,dependent=no,toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0');
}

function calculate_password_strength(password)
{
	 var desc = new Array();
     desc[0] = "Very Weak";
     desc[1] = "Weak";
     desc[2] = "Better";
     desc[3] = "Medium";
     desc[4] = "Strong";
     desc[5] = "Strongest";

     var score   = 0;

     //if password bigger than 6 give 1 point
     if (password.length > 6) score++;

     //if password has both lower and uppercase characters give 1 point
     if ( ( password.match(/[a-z]/) ) && ( password.match(/[A-Z]/) ) ) score++;

     //if password has at least one number give 1 point
     if (password.match(/\d+/)) score++;

     //if password has at least one special caracther give 1 point
     if ( password.match(/.[!,@,#,$,%,^,&,*,?,_,~,-,(,)]/) ) score++;

     //if password bigger than 12 give another 1 point
     if (password.length > 12) score++;

     document.getElementById("passwordDescription").innerHTML = desc[score];
     //document.getElementById("passwordStrength").className = "strength" + score;
}

function reset_password(user_id, type)
{	
  if(type == 1)	
    type = 'affiliate';		
  if(confirm("Reset Password"))	
  {		
    var url = website_url + getURL('panel', 'password', 'admin_reset_done', 'user_id=' + user_id + '&type=' + type);

    window.open( url, 'ResetPassword' ,'width=900,height=800,dependent=no,toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0');	
  }
}

function insertAtCursor(myField, myValue)
{
	myField = $(myField);
	//IE support
	if (document.selection)
	{
		myField.focus();
		sel = document.selection.createRange();
		sel.text = myValue;
	}
	//MOZILLA/NETSCAPE support
	else if (myField.selectionStart || myField.selectionStart == '0')
	{
		var startPos = myField.selectionStart;
		var endPos = myField.selectionEnd;
		myField.value = myField.value.substring(0, startPos)
		+ myValue
		+ myField.value.substring(endPos, myField.value.length);
	} 
	else 
	{
		myField.value += myValue;
	}
}

function setCookie(c_name,value,expiredays)
{
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+
	((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

function getCookie(c_name)
{
	if (document.cookie.length>0)
	{
		c_start=document.cookie.indexOf(c_name + "=");
		if (c_start!=-1)
		{ 
			c_start=c_start + c_name.length+1; 
			c_end=document.cookie.indexOf(";",c_start);
			if (c_end==-1) c_end=document.cookie.length;
			return unescape(document.cookie.substring(c_start,c_end));
		} 
	}
	return "";
}

//setCookie('screen_width', screen.width, 365);
//setCookie('screen_height', screen.height, 365);

function echo($data)
{
  alert($data);
}

var myGlobalHandlers = {
  onCreate: function(){
    Element.show('systemWorking');
},

onComplete: function() {
  if(Ajax.activeRequestCount == 0){
    Element.hide('systemWorking');
  }
  }
};

function processChecked(id)
{
  field = document.getElementById(id);
  chk_name = 'process_array['+id+']';
  
  chk_box = document.getElementById(chk_name);
  
  if (chk_box==null)
  {
    return ;
  }
  
  if(chk_box.checked)
  {
    chk_box.checked = false;
    if (id%2==0)
    field.className = 'even_row';    
    else
    field.className = 'odd_row';
  }
  else
  {
    chk_box.checked = true;
    field.className = 'selected';
  }
}

function set_color(id,color)
{
  field = document.getElementById(id);
  field.className = color;
  
  chk_name = 'process_array['+id+']';
  
  chk_box = document.getElementById(chk_name);
  if (chk_box==null)
  {
    return ;
  }
  
  if(chk_box.checked)
  {
    field.className = 'selected';
  }
  else
  {
    field.className = color;
  }
}

function gEBI(id) {
  return document.getElementById(id);
}

function gEBN(nm, el) {
  return el==undefined ? document.getElementsByName(nm) : el.getElementsByName(nm);
}

function gEBTN(tn, el) {
  return el==undefined ? document.getElementsByTagName(tn) : el.getElementsByTagName(tn);
}

//last modified : 2007-12-14
function makeTextAreaBigger(direction,id) {
  var txtarea = document.getElementById(id);
  if (direction==1) {
    txtarea.cols = txtarea.cols + 10;
    txtarea.rows = txtarea.rows + 3;
  } else {
    txtarea.cols = txtarea.cols - 10;
    txtarea.rows = txtarea.rows - 3;
  }
  //document.getElementById("textarea_next_time").value=txtarea.cols+","+txtarea.rows;
}

//pritesh
//last modified : 2007-12-14


//pritesh
//last modified : 2007-11-29

function isInteger(value)
{
  ///(^-?\d\d*\.\d*$)|(^-?\d\d*$)|(^-?\.\d\d*$)/;
  var regex = /(^\d\d*$)/;
  if (!regex.test(value)) 
  {
    return false;
  }
  else
  {
    return true;
  }
}

function showConfirmationBox(msg)
{ 
  var r = confirm(msg);  
  if(r==true) 
  { 
    return true;
  }
  else 
  { 
    return false;
  }
}

function ShowCheckBoxConfirmation(id,msg)
{
  checked_box = $(id);
  
  if(checked_box.checked)
  {
    var r = showConfirmationBox(msg);
    if(r==false)
    {
      checked_box.checked = '';
    }
  }
}

function ShowHideElement(id,show_id,hide_id,status)
{
  tr= $(id);
  
  show = $(show_id);
  hide = $(hide_id);
  
  if(status==1)
  {
    tr.style.display = "";
    show.style.display = "none";
    hide.style.display = "block";
  }
  else
  {
    tr.style.display = "none";    
    show.style.display = "block";
    hide.style.display = "none";
  }
  
}

function showConfirmationBox(msg)
{ 
    var r = confirm(msg);  
    if(r==true) 
    { 
      return true;
    }
    else 
    { 
      return false;
    }
}

function prepare(form_id) {
var formblock;

formblock= document.getElementById(form_id);
forminputs = formblock.getElementsByTagName('input');

return forminputs;
}

function select_all(formname,name, value, change_color) 
{

  forminputs = prepare(formname);
   
  for (i = 0; i < forminputs.length; i++) 
  {
    // regex here to check name attribute   
    
    var regex = new RegExp(name, "i");
    if (regex.test(forminputs[i].getAttribute('name'))) 
    {
      if (value == '1') 
      {
        forminputs[i].checked = true;
        if (change_color==1)
        {
          set_color(forminputs[i].value,'selected');
        }        
      } 
      else 
      {
        forminputs[i].checked = false;
        if (change_color==1)
        {
          if (forminputs[i].value%2==0)
          set_color(forminputs[i].value,'even_row');
          else
          set_color(forminputs[i].value,'odd_row');
        }        
      }
    }
  }
}

openWins = new Array();
curWin = 0;
function new_openpopup(url,width,height) 
{
   openWins[curWin++] = window.open(
        url, 'new' ,
        'width='+width+',height='+height+',toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0');          
}



function new_opener(url)
{
  opener.location = url;
  self.close();
}

function getXMLValue(xml_response,tag_name)
{
  var xml_element =  xml_response.getElementsByTagName(tag_name);
  var xml_value   =  xml_element.item(0).firstChild.data.toString();
  return xml_value;   
}

////////////// Rahul /////////////////////////
//Captch Reload Code 
function reload_captcha(element)
{
  url=website_url+'captcha.php?'+Math.round(Math.random()*10000+1);
  document.getElementById(element).src=url;
}

//check box
function showDiv(id,text_pos)
{
  div_id = document.getElementById(id);  
  img_div_id = document.getElementById("img_"+id);
  if(div_id.style.display == "none")
  {
   img_div_id.src = icon_path+"drop_cross.gif";
   img_div_id.title = "Click to Close Options";
   div_id.style.display = "block";
   text_element = document.getElementById(text_pos);
   text_element_pos = findPos(text_element);
   
   div_id.style.left = text_element_pos[0];
   
   var browser=navigator.appName;
   if (browser=="Microsoft Internet Explorer")
   {
    div_id.style.top = text_element_pos[1] + 19;
   } 
   else
   {
    div_id.style.top = text_element_pos[1] + 10;
   } 
  }
  else
  {
    div_id.style.display = "none";
    img_div_id.src = icon_path+"drop.gif";
    img_div_id.title = "Click to Select Options";
  }
}

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 hideDiv(id)
{
  div_id = document.getElementById(id);
  if(div_id.style.display == "block")
  {
    div_id.style.display = "none";
  }
}

function showDiv_always(id)
{
  div_id = document.getElementById(id);
  div_id.style.display = "block";
}

function prepare_option(form_id) 
{
var formblock_op;

formblock_op= document.getElementById(form_id);
forminputs = formblock_op.getElementsByTagName('input');

return forminputs;
}

function select_options(formname,name, value) 
{

  forminputs = prepare_option(formname);
  
  for (i = 0; i < forminputs.length; i++) 
  {
    // regex here to check name attribute   
    
    var regex = new RegExp(name, "i");
    //alert(forminputs[i].getAttribute('name'));
    if (regex.test(forminputs[i].getAttribute('name')) || 1) 
    {
      //alert(forminputs[i]);
      if (value == 1) 
      {
         //alert('select');
        forminputs[i].checked = true;
      } 
      else 
      {
        forminputs[i].checked = false;
      }
    }
  }
}

function SelectAll(id)
{
    document.getElementById(id).focus();
    document.getElementById(id).select();
}

function autoFill(from_id, to_id)
{
  var to_field = document.getElementById(to_id);
  var from_field = document.getElementById(from_id)
  if(to_field.value == '')
  {
    from_field = from_field.value.replace(/[^a-zA-Z 0-9]+/g,'');
  	from_field = from_field.toLowerCase();
  	from_field = String.replace(from_field," ","-");
  	to_field.value = from_field;
  }	
}

function printPartOfPage(elementId,hidden_field)
{
  
 var hidden = document.getElementById(hidden_field);
 
 var printContent = document.getElementById(elementId);
 var windowUrl = 'about:blank';
 var uniqueName = new Date();
 var windowName = 'Print' + uniqueName.getTime();
 var printWindow = window.open(windowUrl, windowName, '');
 
 printWindow.document.write(hidden.innerHTML); 
 printWindow.document.write(printContent.innerHTML);
 printWindow.document.close();
 printWindow.focus();
 printWindow.print();
 printWindow.close();
}

//////////////// rahul //////////////////////

function showhide(id)
{
  
  if(document.getElementById(id).style.display == 'none')
  {
    document.getElementById(id).style.display = '';
    document.getElementById('showhideDiv').innerHTML = 'Hide Result';
  }
  else
  {
    document.getElementById(id).style.display = 'none';
    document.getElementById('showhideDiv').innerHTML = 'Show Result';
  }
}

function showhideTr(id)
{
  
  if(document.getElementById(id).style.display == 'none')
  {
    document.getElementById(id).style.display = '';
  }
  else
  {
    document.getElementById(id).style.display = 'none';
  }
}

function show_lightwindow(href,title,params)
{	
	myLightWindow.activateWindow({
	href: href,
	title: title,
	params: params
	});
}


//We are using this function in a displaycontent.html and register.html
function iAccept(id)
{
	if(id == 'iAccept')
	{
		if(document.getElementById('mypassword').style.display == 'none')
		{
			document.getElementById('mypassword').style.display = "";
		}
		else
		{
			document.getElementById('mypassword').style.display = "none";
		}
	}
	else
	{
		parent.document.getElementById('iaccept').checked = true;
		parent.document.getElementById('mypassword').style.display = "";
		parent.myLightWindow.deactivate();
	}
}

//Function Used For Adding one select box value in to other select box
function addSelect( select_box, add_select_box , remove_val )
{
  var val = document.getElementById(select_box).options;
  var add_select_box = document.getElementById(add_select_box);

  for(i=0;i<val.length;i++)
  {
    if(val[i].selected==true)
    {
      var star = document.createElement('option');
      
      if(AddOption(add_select_box,val[i].text,val[i].value)==true)
      {
        if( remove_val == true)
        {
          val[i] = null;    
        }        
      }               
    }
  }
}

//Function Used For Removing select box value.
function RemoveOption(id)
{
  var theSel = document.getElementById(id);
  var selIndex = theSel.selectedIndex;
  
  if (selIndex != -1) {
    for(i=theSel.length-1; i>=0; i--)
    {
      if(theSel.options[i].selected)
      {
        theSel.options[i] = null;

      }
    }
    if (theSel.length > 0) 
    {
      theSel.selectedIndex = selIndex == 0 ? 0 : selIndex - 1;
    }
  }
}

//This Function add Categories In Other Multiselect box
function AddOption(selectbox,text,value)
{
  var old_val = selectbox.options;
  var temp = value;
  var flag = true;
  
  for(j=0;j<old_val.length;j++)
  {
    if(old_val[j].value==temp)
    {
      flag = false;
      alert(text+' Already Present In List');
      return flag;      
    }    
  }
  
  if(flag==true)
  {
    var optn = document.createElement("OPTION");
    optn.text = text; 
    optn.value = value;
    optn.selected = flag;
    selectbox.options.add(optn);
  } 
  return flag;       
}

function SetToFirstControl()
{
  var bFound = false; 

  //for each form
  for (f=0; f < document.forms.length; f++) 
  {
    //for each element in each form
    for(i=0; i < document.forms[f].length; i++)
    {
    	
      //if it's not a hidden element
      if (document.forms[f][i].type != "hidden" && document.forms[f][i].type != "select-one") 
      { 
        //and it's not disabled
        if (document.forms[f][i].disabled != true) 
        {
          try {
             //set the focus to it
             document.forms[f][i].focus();
             var bFound = true;
          }
          catch(er) {
          }
        }
      }
      //if found in this element, stop looking
      if (bFound == true)
        break;
    }
    //if found in this form, stop looking
    if (bFound == true)
      break;
  }
}

