// Global Variables
//this loops over the photo playlist
var playlist_counter=0;
var no_more_shipping=false;
//
//User Admin Functions
// Promoting
function updatePreview(form_field_id,preview_field_id){
	$(preview_field_id).innerHTML=$(form_field_id).value;

	
}
// End Promoting
// Utility Functions
function trim(str, chars) {
    return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}
function wordwrap( str, int_width, str_break, cut ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // +   improved by: Nick Callen
    // +    revised by: Jonas Raoni Soares Silva (http://www.jsfromhell.com)
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Sakimori
    // *     example 1: wordwrap('Kevin van Zonneveld', 6, '|', true);
    // *     returns 1: 'Kevin |van |Zonnev|eld'
    // *     example 2: wordwrap('The quick brown fox jumped over the lazy dog.', 20, '<br />\n');
    // *     returns 2: 'The quick brown fox <br />\njumped over the lazy<br />\n dog.'
    // *     example 3: wordwrap('Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.');
    // *     returns 3: 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod \ntempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim \nveniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea \ncommodo consequat.'
 
    // PHP Defaults
    var m = ((arguments.length >= 2) ? arguments[1] : 75   );
    var b = ((arguments.length >= 3) ? arguments[2] : "\n" );
    var c = ((arguments.length >= 4) ? arguments[3] : false);
 
    var i, j, l, s, r;
 
    str += '';
 
    if (m < 1) {
        return str;
    }
 
    for (i = -1, l = (r = str.split("\n")).length; ++i < l; r[i] += s) {
        for(s = r[i], r[i] = ""; s.length > m; r[i] += s.slice(0, j) + ((s = s.slice(j)).length ? b : "")){
            j = c == 2 || (j = s.slice(0, m + 1).match(/\S*(\s)?$/))[1] ? m : j.input.length - j[0].length || c == 1 && m || j.input.length + (j = s.slice(m).match(/^\S*/)).input.length;
        }
    }
 
    return r.join("\n");
}
//End Utility Functions
//Text Areas MaxLength functions
function setMaxLength() {

	var x = document.getElementsByTagName('textarea');

	var counter = document.createElement('div');

	counter.className = 'counter';

	for (var i=0;i<x.length;i++) {

		if (x[i].getAttribute('maxlength')) {

			var counterClone = counter.cloneNode(true);

			counterClone.relatedElement = x[i];

			counterClone.innerHTML = '<span>0</span>/'+x[i].getAttribute('maxlength');

			x[i].parentNode.insertBefore(counterClone,x[i].nextSibling);

			x[i].relatedElement = counterClone.getElementsByTagName('span')[0];



			x[i].onkeyup = x[i].onchange=checkMaxLength;

			x[i].onkeyup();

		}

	}

}



function checkMaxLength() {

	var maxLength = this.getAttribute('maxlength');

	var currentLength = this.value.length;

	if (currentLength > maxLength){

		this.value=this.value.substring(0,maxLength);

		this.relatedElement.className = 'toomuch';

	}

	else

		this.relatedElement.className = 'fine';

	this.relatedElement.firstChild.nodeValue = currentLength;

	// not innerHTML

}
function showReplies(id){
	if($('reply_link_'+id).innerHTML=="Expand"){
		Effect.BlindDown('comment_replies_'+id);
		$('reply_link_'+id).innerHTML="Collapse";
	}
	else if($('reply_link_'+id).innerHTML=="Collapse"){
		Effect.BlindUp('comment_replies_'+id);
		$('reply_link_'+id).innerHTML="Expand";
	}
}
//End TextArea MaxLength Functions
//End User Admin functions

//Front End Functions
function loadPhotos(total_photos){
	for(i=0;i<total_photos;i++){
		$('photo_'+i).style.display='none';
		}
	//
	Effect.Appear('photo_'+playlist_counter);
	playlist_counter++;
	if(playlist_counter==total_photos)
		playlist_counter=0;
	
	}
// It Hides submit buttons and shows buttons to send Ajax requests instead of posting forms
function hideSubmitButtons(){
	if($('post_comment_submit'))
		$('post_comment_submit').style.display='none';
	if($('post_comment_button'))
		$('post_comment_button').style.display='block';
}	
 function selDeSel(total_contacts){



for(i = 0; i < total_contacts; i++){
		if($('group_select').checked==true)
			$('contact_'+i).checked = true;
		else
			$('contact_'+i).checked = false;	
	}
}
function selDeSelContacts(){
var total_contacts=$('total_contacts').value;
for(i = 0; i < total_contacts; i++){
		if($('group_select').checked==true)
			$('contact_'+i).checked = true;
		else
			$('contact_'+i).checked = false;	
	}	
}
function enableField(chbx_id,field_id){
	if($(chbx_id).checked){
		$(field_id).disabled=false;
	}
	else{
		$(field_id).disabled=true;
	}
}
function populateTags(total_tags){
	var tags='';
	var i=1;
	while(i<=total_tags){
		
		if($('existing_tags_'+i).checked)
			tags+=$('existing_tags_'+i).value;
		i++;
		if(i<=total_tags)
			if($('existing_tags_'+i).checked && tags!='')
				tags+=',';	
	}
	if($('new_tags').disabled==false){
		if(tags!=''&&$('new_tags').value!='')
		tags+=',';
	tags+=$('new_tags').value;
	}
	$('selected_tags').innerHTML=tags;
	$('tags_selected').value=tags;
}
function validateTagsForm(total_tags){
	var valid=false;
	$('error_message_tags').style.display='none';
	$('error_message_comment').style.display='none';
	if(trim($('selected_tags').innerHTML)==''){
		$('error_message_tags').innerHTML	=	"Please select atleast one existing tag, or add new tags";
		Effect.Appear('error_message_tags');
		return valid;
	}
	else if(trim($('comment').value)==''){
		$('error_message_comment').innerHTML	=	"Please add a comment/review.";
		Effect.Appear('error_message_comment');
		return valid;
	}
	else{
		valid=true;
		return valid;
	}
	
}
function enableEditor(baseUrl,text_area)
    {
        if(document.getElementById(text_area)) {
            var oFCKeditor = new FCKeditor(text_area) ;
            oFCKeditor.BasePath = baseUrl+"/public/scripts/fckeditor/" ;
            oFCKeditor.Height = 500;
            oFCKeditor.ReplaceTextarea() ;
        }
    }
//End Front End Functions
///////////// User Admin Fucntions ////////////////////////////////
//Selling Format Page /////////////////////
function showDetails(){
	$('declining_format').style.display='none';
	$('fixed_format').style.display='none';
	$('auction_format').style.display='none';
	$('start_price').className	=	'';
	$('min_price').className	=	'';
	$('fixed_price').className	=	'';
	$('starting_bid').className		=	'';
	$('reserved_price').className	=	'';
	
	if($('format_type_declining').checked){
		Effect.Appear('declining_format');
		$('start_price').className	=	'required';
		$('min_price').className	=	'required';
		}
	else if($('format_type_fixed').checked){
		Effect.Appear('fixed_format');
		$('fixed_price').className	=	'required';
		}
	else if($('format_type_auction').checked){
		Effect.Appear('auction_format');
		$('starting_bid').className		=	'required';
		$('reserved_price').className	=	'required';
		
		}
	else
		return;				
	//var format	=	$('format_type_auction').value;
	
}

function addRowToTable(tblId)
{
  if(no_more_shipping){
  	alert('No more shipping destinations available');
  	return ;
  }
  var selDefault=	document.getElementById('shipRow0');
  if(selDefault.value=='all'){
  	alert('To Add a new shipping please deselect all from default shipping.');
  	return ;
  }
  
  var tbl = document.getElementById(tblId);
  var lastRow = tbl.rows.length;
  // if there's no header row in the table, then iteration = lastRow + 1
  var flag=false;
  var iteration = lastRow-1;
  //alert(lastRow);
  var row = tbl.insertRow(lastRow);
  //alert(iteration);
  
  
  var cellLeft = row.insertCell(0);
  var textNode = document.createTextNode(lastRow+'.');
  cellLeft.appendChild(textNode);
 // first cell
  var firstCell = row.insertCell(1);
  var sel = document.createElement('select');
  sel.name = 'shipRow' + iteration;
  sel.id	= 'shipRow' + iteration;
  j=0;
  for(i=1;i<selDefault.options.length;i++){
  	for(k=0;k<iteration;k++){
  		
		if(selDefault.options[i].value==document.getElementById('shipRow'+k).value){
			flag=true;
		}	
  	}
  	if(flag==false){	
		sel.options[j]=	new Option (selDefault.options[i].text,selDefault.options[i].value);
		j++;
		
	}
	flag=false;
  }
	//sel.options[j] = new Option("Everywhere Else","else");
	if(j==0)
		no_more_shipping=true;		
  firstCell.appendChild(sel);
  
  // second cell
  var secondCell = row.insertCell(2);
  var el = document.createElement('input');
  el.type = 'text';
  el.name = 'priRow' + iteration;
  el.id = 'priRow' + iteration;
  el.size = 10;
  el.className='required';
  
  secondCell.appendChild(el);
  
// 3rd cell
  var thirdCell = row.insertCell(3);
  var el = document.createElement('input');
  el.type = 'text';
  el.name = 'secRow' + iteration;
  el.id = 'secRow' + iteration;
  el.size = 10;
  el.className='required';
  thirdCell.appendChild(el);
  $('total_rows').value=lastRow;
}
function keyPressTest(e, obj)
{
  var validateChkb = document.getElementById('chkValidateOnKeyPress');
  if (validateChkb.checked) {
    var displayObj = document.getElementById('spanOutput');
    var key;
    if(window.event) {
      key = window.event.keyCode; 
    }
    else if(e.which) {
      key = e.which;
    }
    var objId;
    if (obj != null) {
      objId = obj.id;
    } else {
      objId = this.id;
    }
    displayObj.innerHTML = objId + ' : ' + String.fromCharCode(key);
  }
}
function removeRowFromTable(tblId)
{
  var tbl = document.getElementById(tblId);
  var lastRow = tbl.rows.length;
  if (lastRow > 2){ 
  	tbl.deleteRow(lastRow - 1);
  	no_more_shipping=false;
  	$('total_rows').value=lastRow - 2;
  	}
}
function openInNewWindow(frm)
{
  // open a blank window
  var aWindow = window.open('', 'TableAddRowNewWindow',
   'scrollbars=yes,menubar=yes,resizable=yes,toolbar=no,width=400,height=400');
   
  // set the target to the blank window
  frm.target = 'TableAddRowNewWindow';
  
  // submit
  frm.submit();
}
function validateRow(frm)
{
  var chkb = document.getElementById('chkValidate');
  if (chkb.checked) {
    var tbl = document.getElementById('tblSample');
    var lastRow = tbl.rows.length - 1;
    var i;
    for (i=1; i<=lastRow; i++) {
      var aRow = document.getElementById('txtRow' + i);
      if (aRow.value.length <= 0) {
        alert('Row ' + i + ' is empty');
        return;
      }
    }
  }
  openInNewWindow(frm);
}
function show_hide_order_details(anchorID){
	if($('details_link_'+anchorID).innerHTML=="[+]"){
		Effect.SlideDown('details_'+anchorID);
		$('details_link_'+anchorID).innerHTML="[-]";
	}
	else if($('details_link_'+anchorID).innerHTML=="[-]"){
		Effect.SlideUp('details_'+anchorID);
		$('details_link_'+anchorID).innerHTML="[+]";
	}
	}
function submitSearchForm(cat_id){
	//alert(cat_id);
	//return ;
	$('search_form').category.value=cat_id;
	$('search_form').submit();
}	
function changeImage(baseUrl,img_name){
	var img_html	=	'<img src="'+baseUrl+'/public/images/products/'+img_name+'" />';
	$('product_img').style.visibility="hidden";
	$('product_img').innerHTML=img_html;
	$('product_img').style.visibility="visible";
}
function changePageImage(baseUrl,img_name){
	var img_html	=	'<img src="'+baseUrl+'/public/images/pages/'+img_name+'" />';
	$('main_img').style.visibility="hidden";
	$('main_img').innerHTML=img_html;
	$('main_img').style.visibility="visible";
}	
///////// End Selling Format Page ///////////////////
////////////// End User Admin Functions //////////////////
///////////////////// Shopping Cart / Store Functions ////////////////
function applyFilter(){
	var  url	=	$('filter').value;
	window.location.href	=	url;
}
// Employee Admin Functions ////////////////////////////////////
function confirmDelete(form_id){
	if(confirm("Are you sure, You want to delete?"))
		$(form_id).submit();
	
	else 
		return ;
	
}

// End Employee Admin Functions //////////////////////////

//confirm for importing the new recap games
function confirmImport(form_id){
	if(confirm("Are you sure, You want to Import?"))
		$(form_id).submit();
	
	else 
		return ;
	
}




// Validation Functions ///////////////////////
function validateFriends(){
	var total_contacts	=	$('total_contacts').value;
	var checked_emails	=	0;
	if(total_contacts==0){
		$('validation_feedback').innerHTML	=	"You don't have any contacts";
		return false;
	}	
	else{
		for(i=0;i<total_contacts;i++){
			if($('contact_'+i).checked==true)
				checked_emails++;
		}
	}
	if(checked_emails>0)
		return true;
	else{
		$('validation_feedback').innerHTML	=	"Please select atleast one contact.";
		return false;
	}			
}
// End Validation Functions /////////////////////

///////////////////////////////////////////////////
function showColumnistDiv(id) {
$('columnist_table1').style.display='none'
$('columnist_table2').style.display='none'
$('columnist_table3').style.display='none'
$('columnist_table4').style.display='none'

$('columnist_table'+id).style.display=''
}
//////////////////////////////////////////////////

<!-- function for show/hide gamecenter contents.
var state = 'none';
function showhide(layer_ref) {
	if (state == 'block') {
		state = 'none';
	}
	else {
	state = 'block';
	}
	if (document.all) { //IS IE 4 or 5 (or 6 beta)
		eval( "document.all." + layer_ref + ".style.display = state");
	}
	if (document.layers) { //IS NETSCAPE 4 or below
		document.layers[layer_ref].display = state;
	}
	if (document.getElementById &&!document.all) {
		hza = document.getElementById(layer_ref);
		hza.style.display = state;
	}
}
//-->