/*******************************************************************************************/
/** FILE NAME			: functions.js
/** CREATION DATE		: 24-Feb-2008
/** CREATED BY			: APEX DIVISION DEVELOPERS
/** COPYRIGHT			: @APEXDIVISION
/** DESCRIPTION			: Javascript Functions Enabled with Prototype
/*******************************************************************************************/



/***************************  General Javascript Functions ******************************/



// 1. Function To Keep The Form Value selected on load and clear on focus

function clearDefault(el) {
  if (el.defaultValue==el.value) el.value = ""
}


// 2. Function to open Pop Up

function win_open(sUrl)
{
	window.open(sUrl, '150x250', 'toolbar=no, status=no, scrollbars=yes, location=no, resizable=1, menubar=no, width=600, height=400');
}

function customizedPopUp(sUrl,height,width)
{
	// alert('toolbar=no, status=no, scrollbars=yes, location=no, resizable=0, menubar=no, width='+width+', height='+height+'');
	window.open(sUrl, 'window', 'toolbar=no, status=no, scrollbars=yes, location=no, resizable=0, menubar=no, width='+width+', height='+height+'');
}


// 3. Javascript version of the PHP base64_encode

function base64_encode( data ) {
    
 
    var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
    var o1, o2, o3, h1, h2, h3, h4, bits, i=0, enc='';
 
    do { // pack three octets into four hexets
        o1 = data.charCodeAt(i++);
        o2 = data.charCodeAt(i++);
        o3 = data.charCodeAt(i++);
 
        bits = o1<<16 | o2<<8 | o3;
 
        h1 = bits>>18 & 0x3f;
        h2 = bits>>12 & 0x3f;
        h3 = bits>>6 & 0x3f;
        h4 = bits & 0x3f;
 
        // use hexets to index into b64, and append result to encoded string
        enc += b64.charAt(h1) + b64.charAt(h2) + b64.charAt(h3) + b64.charAt(h4);
    } while (i < data.length);
 
    switch( data.length % 3 ){
        case 1:
            enc = enc.slice(0, -2) + '==';
        break;
        case 2:
            enc = enc.slice(0, -1) + '=';
        break;
    }
 
    return enc;
}



// 4. Javascript version of the PHP base64_decode

function base64_decode( data ) {
    
 
    var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
    var o1, o2, o3, h1, h2, h3, h4, bits, i=0, enc='';
 
    do {  // unpack four hexets into three octets using index points in b64
        h1 = b64.indexOf(data.charAt(i++));
        h2 = b64.indexOf(data.charAt(i++));
        h3 = b64.indexOf(data.charAt(i++));
        h4 = b64.indexOf(data.charAt(i++));
 
        bits = h1<<18 | h2<<12 | h3<<6 | h4;
 
        o1 = bits>>16 & 0xff;
        o2 = bits>>8 & 0xff;
        o3 = bits & 0xff;
 
        if (h3 == 64)      enc += String.fromCharCode(o1);
        else if (h4 == 64) enc += String.fromCharCode(o1, o2);
        else               enc += String.fromCharCode(o1, o2, o3);
    } while (i < data.length);
 
    return enc;
}


// 5. Validate Email Format

function CheckEmail(email) {
	AtPos = email.indexOf("@")
	StopPos = email.lastIndexOf(".")
	Message = ""

	/*if (email == "") {
		return false;
	}*/

	if (AtPos == -1 || StopPos == -1) {
		return false;
	}

	if (StopPos < AtPos) {
		return false;
	}

	if (StopPos - AtPos == 1) {
		return false;
	}

	return true;
}


// 6. Hide A Div

function hideDiv(ID,SecID,ThiID){
	$(ID).hide();
	$(SecID).value = '';
	$(ThiID).value = '';
}

// 7. Toggle Display

function showToggle(ID)
{
	if($(ID).style.display == "none")
	{
		$(ID).show();
	}
	else
	{
		$(ID).hide();
	}
}

// 8. Check during deleting mass data by checkbox

function deleteCheck()
{

	//alert('Hi');
	check=false
	for(var i=0;i<=frm_list.elements.length-1;i++)
		{
			//alert("checkFOr");
			if (document.frm_list.elements[i].checked){
				check=true;
				break;
			}
		}
	if(check == false)
	{
		alert("You have not choosen any data");
		return false;
		
	}
	else
	{
		return confirm('Do You Really Want To Delete');
		
		
	}

}
 
// 9. Toggle select unselect checkbox

function checkAll(ref) {
//	alert("rr"+ref);
  var chkAll = document.getElementById('checksAll');
  var checks = document.getElementsByName('del[]');
 
 var boxLength = checks.length;
  var allChecked = false;
  
  if ( ref == 1 ) {
    if ( chkAll.checked == true ) {
      for ( i=0; i < boxLength; i++ ) {
        checks[i].checked = true;
      }
    }
    else {
      for ( i=0; i < boxLength; i++ ) {
        checks[i].checked = false;
      }
    }
  }
  else {
    for ( i=0; i < boxLength; i++ ) {
      if ( checks[i].checked == true ) {
        allChecked = true;
        continue;
      }
      else {
        allChecked = false;
        break;
      }
    }
    if ( allChecked == true ) {
      chkAll.checked = true;
    }
    else {
      chkAll.checked = false;
    }
  }
  
}

// 10. Toggle Copying Value by selecting and deselecting a checkbox


function toggleCheck(ch)
{
	if(ch == true)
	{
		$('shipping_salutation').value = $('billing_salutation').value;
		$('sfname').value = $('fname').value;
		$('slname').value = $('lname').value;
		$('saddress').value = $('address').value;
		$('scity').value = $('city').value;
		$('sprovince').value = $('province').value;
		$('scountryID').value = $('countryID').value;
		$('spcode').value = $('pcode').value;
		$('semail').value = $('email').value;
		$('sphone').value = $('phone').value;
		$('smobile').value = $('mobile').value;
	}else if(ch == false)
	{
		$('shipping_salutation').value = '';
		$('sfname').value = '';
		$('slname').value = '';
		$('saddress').value = '';
		$('scity').value = '';
		$('sprovince').value = '';
		$('scountryID').value = '';
		$('spcode').value = '';
		$('semail').value = '';
		$('sphone').value = '';
		$('smobile').value = '';
	}
}


// 12. Same Function as above for admin Panel use


function toggleCheckAdmin(ch)
{
	if(ch == true)
	{
		$('ssalutation').value = $('salutation').value;
		$('sfname').value = $('fname').value;
		$('slname').value = $('lname').value;
		$('saddress').value = $('address').value;
		$('scity').value = $('city').value;
		$('sprovince').value = $('province').value;
		$('scountryID').value = $('countryID').value;
		$('spost_code').value = $('post_code').value;
		$('semail').value = $('email').value;
		$('sphone').value = $('phone').value;
		$('smobile').value = $('mobile').value;
	}else if(ch == false)
	{
		$('ssalutation').value = '';
		$('sfname').value = '';
		$('slname').value = '';
		$('saddress').value = '';
		$('scity').value = '';
		$('sprovince').value = '';
		$('scountryID').value = '';
		$('spost_code').value = '';
		$('semail').value = '';
		$('sphone').value = '';
		$('smobile').value = '';
	}
}


function send_param()
{
	var keys = $('tags').value;
	var encode = base64_encode(keys);
	window.location.href = 'search.php?search_cond=Search&keywords='+encode;
}


function displayImage(src){
	var src = src;
	$('variant_images').style.visibility = 'visible';
	$('variant_image').src = src;
}

function hideImage()
{
	$('variant_images').style.visibility = 'hidden';
}

/* Function For character count */

function getObject(obj) {
  var theObj;
  if(document.all) {
    if(typeof obj=="string") {
      return document.all(obj);
    } else {
      return obj.style;
    }
  }
  if(document.getElementById) {
    if(typeof obj=="string") {
      return document.getElementById(obj);
    } else {
      return obj.style;
    }
  }
  return null;
}

function toCount(entrance,exit,text,characters) {
  var entranceObj=getObject(entrance);
  var exitObj=getObject(exit);
  var length=characters - entranceObj.value.length;
  if(length <= 0) {
    length=0;
    text='<span"> '+text+' </span>';
    entranceObj.value=entranceObj.value.substr(0,characters);
  }
  //exitObj.innerHTML = text.replace("{CHAR}",length);
  exitObj.innerHTML = length+" characters left";
}



/***************************  General Javascript Functions Ends ******************************/

