/*
 * show orange button
 */
 function show_button (target_id, text, link, onclick, end_text, id) {
 	if (text == '') {
		return '';
	} else {
		if (link == '' || link == undefined ) {
			link = '#';
		}
		if (onclick == '' || onclick == undefined) {
			onclick = ''; 
		} else {
			onclick='onclick="'+onclick+';return false;"';
		}
		if ( end_text== '' || end_text == undefined) {
			end_text = '';
		}
	
 	document.getElementById(target_id).innerHTML = '<table class="butns" >'+
						'<tr>'+
								'<td><img src="images/left_back_btn.jpg" alt="" /></td>'+
								'<td nowrap="nowrap"  class="back_btn" ><a id="'+id+'" '+onclick+' href="'+link+'">'+text+'</a></td>'+
								'<td><img src="images/right_back_btn.jpg" alt="" /></td>'+
						'</tr>'+
				'</table>'+end_text;
	}
 }
 
sfFocus = function(sfEls) {
	for (var i=0; i<sfEls.length; i++) {
		sfEls[i].onfocus=function() {
			this.className+=" sffocus";
		}
		sfEls[i].onblur=function() {
			this.className=this.className.replace(new RegExp(" sffocus\\b"), "");
		}
	}
}
function suckerfish(type, tag, parentId) {
	if (window.attachEvent) {
		window.attachEvent("onload", function() {
			var sfEls = (parentId==null)?document.getElementsByTagName(tag):document.getElementById(parentId).getElementsByTagName(tag);
			type(sfEls);
		});
	}
}

function check_fields() {
	for (i = 1; i < req_array.length; i = i + 1) {
		if ($('#' + req_array[i]).val() == '') {
			alert('Please enter required filed "' + req_message[i] + '"');
			return false;
		}
	}
	return true;
}
/** make bind for buttons
 * 
 * @param string flag ( '', 'profile_edit')
 */
function make_bind(flag, default_country ) {
	

	$('#button1').bind('click', function(){
		//Clear field name
		if( $('#r_name').val()=="First and Last Name" ) {
			$('#r_name').val('');
		};
		
		if (!$('#accept').attr('checked') || flag == 'profile_edit') {
			if (!check_fields()) {
				return false;
			};
			if (flag != 'profile_edit') {
				$('#div_message').html('<strong style=\"color:red\">Please agree to the terms and conditions</strong>').show().fadeIn();
				$('#div_message').fadeOut(4000);
				return false;
			}
		} 

		if ($('#accept2').attr('checked')!=undefined && (!$('#accept2').attr('checked') || flag == 'profile_edit')) {
			if (!check_fields()) {
				return false;
			};
			if (flag != 'profile_edit') {
				$('#div_message').html('<strong style=\"color:red\">Please agree to all the terms and conditions</strong>').show().fadeIn();
				$('#div_message').fadeOut(4000);
				return false;
			}
		} 

		if (check_fields()) {
			$('#reg_form1').submit();	
		}
				
		
	});
	
	if (flag != 'profile_edit') {
		$('#button2').bind('click', function(){
			if ($('#login_email').val() == '' ) {
				alert('Please input email');
				return false;
			}
			if ($('#login_password').val() == '' ) {
				alert('Please input password');
				return false;
			}
			$('#reg_form2').submit();
		});
	}
	
	UsStates(default_country);
}


function UsStates (default_country) {
	if ($('#countries').val() == default_country) {
		$('#us_states').show();
	}
	else {
		$('#us_states').hide();
	}
	
	$('#countries').bind('change blur', function(){
		if ($('#countries').val() == default_country) {
			$('#us_states').show();
		}
		else {
			$('#us_states').hide();
		}
	});
}


//if (window.attachEvent) window.attachEvent("onload", sfFocus);

/**
 * Get search date range 'yyyy-mm-dd,yyyy-mm-dd':
 * date dropDown or pick
 * @return string 
 */
function getSearchDateRange()
{
	var dateRange;
	
	if( $('#dropDownChoice') && $('#dropDownChoice').attr('checked') ) {
		dateRange=$('#searchDateDropDown').val();
	} else if( $('#pickDateChoice') && $('#pickDateChoice').attr('checked') ) {
		dateRange=$('#firstDateId').val()+','+$('#secondDateId').val();
	} else {
		dateRange='null';
	}
 	return dateRange;
}

/**
 * Update variable through Ajax(json)
 * 
 * @param url string
 * @param id int
 * @param status bool,but can be int
 * @return bool
 */
function updateVariableAjax(url,id,value)
{	
	return updateVariableFieldAjax(url,id,value,null);
}

/**
 * Update variable through Ajax(json) + field name
 * 
 * @param url string
 * @param id int
 * @param status bool,but can be int
 * @param name string|null field name for update
 * @return bool
 */
function updateVariableFieldAjax(url,id,value,name)
{	
	var resultStatus=false;
	var data={variable:value,id:id};
	if( name != null ) {
		data.name=name;
	}
	$.getJSON(url,data,function(json){
	    if( json.status == true ) {
	        resultStatus=true;
	    } else {
	        alert(json.result);
	    }
	})
	return resultStatus;
}

/**
 * Send json data (array) through ajax.
 * 
 * @param dataArray JSON 
 * @param type string (add,remove etc.)
 * @return bool
 */
function setDataAjax(url,dataArray,type)
{
	var resultStatus=false;
	$.ajaxSetup({dataType:"json"});
    $.post(url,{data:$.toJSON(dataArray),todo:type},function(json){
    	if( json.status == true ) {
	        resultStatus=true;
	    } else {
	        alert(json.result);
	    }
    });
    return resultStatus;
}

function adPreview(element,adId)
{
	var iframe;
	$(element).hover(function() { 
		if(!iframe) {
			iframe=document.createElement('iframe');
			$(iframe).attr({src:'/preview/ad?adId='+adId,frameBorder:0,scrolling:"no"})
				     .css({display:"block",position:"absolute",zIndex:"3000",border:0});
			$(iframe).bind('load',function() {
				var objSizes;
				if(iframe.contentWindow.document.getElementById('adBlock')) {
					objSizes=iframe.contentWindow.document.getElementById('adBlock');
				} else {
					objSizes=iframe.contentWindow.document.body;
				}

				var widthI=objSizes.clientWidth;
				var heightI=objSizes.clientHeight;
				//alert('width:'+widthI+' height:'+heightI);
				//alert( iframe.contentWindow.document.getElementById('adBlock').clientHeight  );
				$(iframe).css({width:widthI,
						        height:heightI});
			});
			$(iframe).appendTo(element);	
			
		} else {
			$(iframe).css({display:'block'});
		}
	},function () {
		$(iframe).css({display:'none'});
	});
}

function SelectBoxes(left,right,options) {
	//Init variables
	this.left=left;
	this.right=right;
	this.postUrl='#';
	this.leftRightAction='add';
	this.rightLeftAction='remove';
	this.outComeAttrs=new Object();
	this.fillUrl="#";
	this.titleId=null;
	var _selfObject=this;
	
	if(options.postUrl) {
		this.postUrl=options.postUrl;
	}
	
	if(options.fillUrl) {
		this.fillUrl=options.fillUrl;
	}
	
	if(options.leftRightAction) {
		this.leftRightAction=options.leftRightAction;
	}
	
	if(options.rightLeftAction) {
		this.rightLeftAction=options.rightLeftAction;
	}
	if(options.outComeAttrs) {
		this.outComeAttrs=options.outComeAttrs;
	}
	if(options.titleId) {
		this.titleId=options.titleId;
	}
	
	this.moveElementLeftRight=function() {
		this._moveElementFromTo(this.left, this.right, this.leftRightAction);
	};
	this.moveElementsLeftRight=function() {
		this._moveElementsFromTo(this.left, this.right, this.leftRightAction);
	};
	this.moveElementRightLeft=function() {
		this._moveElementFromTo(this.right, this.left, this.rightLeftAction);
	};
	
	this.moveElementsRightLeft=function() {
		this._moveElementsFromTo(this.right, this.left, this.rightLeftAction);
	};
	
	this.moveElementsFromTo=function(from,to,action) {
		this._moveElementFromTo(from,to,action);
	};
	
	this.fillBoxes=function(postData) {
		$(this.left).html('');
	    $(this.right).html('');
	    
		if(!postData) {
			return;
		}
		
		$.post(this.fillUrl, postData, function(json) {
	    	 	$(json.leftBox).each(function() {
	    	 		$(_selfObject.left).append( _selfObject._getOptionForAppend(this) );
		        });
		        $(json.rightBox).each(function() {
		            $(_selfObject.right).append( _selfObject._getOptionForAppend(this) ); 
		        });        
		    }, "json");
		
	};
	
	this._changeBlocks=function(dataArray,type) {
	   $.post(this.postUrl,{data:$.toJSON(dataArray),todo:type}, function(json) {
         if(json.status!=true) {
             alert(json.result);
         }
       },'json');
	};
	
	this._moveElementFromTo=function(from,to,action) {
		var data=new Array();
		   $('option:selected',from).each(
		    function(){
		        $(to).append(this);
		        data[data.length]=_selfObject._getOptionOutComeAttrs(this);
		    }
		);
	    this._changeBlocks(data,action);
	};
	
	this._moveElementsFromTo=function(from,to,action) {
		var data=new Array();
	    $('#'+from+' option').each(
	    function(){
	        $('#'+to).append(this);
	        data[data.length]=this._getOptionOutComeAttrs(this);
	    }
	    );
	    this._changeBlocks(data,action);
	};
	
	this._getOptionOutComeAttrs=function(option) {
		var obj=new Object();
		for(var i=0;i<this.outComeAttrs.length;i++) {
			obj[this.outComeAttrs[i]]=$(option).attr(this.outComeAttrs[i]);
		}
		return obj;
	};
	
	this._getOptionForAppend=function(jsonObject) {
		var str="<option ";
		for(var i=0;i<this.outComeAttrs.length;i++) {
			str+=this.outComeAttrs[i]+"="+jsonObject[this.outComeAttrs[i]]+" ";
		}
		str+=" >";
		if(this.titleId) {
			str+=" "+jsonObject[this.titleId]+" - ";
		}
		str+=jsonObject.title+"</option>";
		return str;
	}
}
