$( document ).ready( function () {
	window.scrollTo( 0, 0 );
		
	// Focus on first form element
	$( ':text:visible:enabled:first').focus();
	
	// Disable autocomplete feature on all forms
	var disable_autocomplete = function() {
		$( 'form' ).each( function() { 
			$( this ).attr( 'autocomplete', 'off' );
		} );
	}();

	// Disable radio buttons on VIP form
	var disable_radio_buttons = function() {		
		$( '#vip_form input:radio' ).each( function() {
			$( this ).attr( 'disabled', 'disabled' );
			
			if ( $( this ).val() == 0 ) {
				$( this ).attr( 'checked', 'checked' );
			}
		} );	
	}
	
	if ( $( '#package' ).val() == '' || $( '#package' ).val() == 2 ) {
		disable_radio_buttons();
	}
	
	
	// Package Type feature
	$( '#package' ).change( function() {
		if ( $( '#package option:selected ' ).val() == 1 ) {
			$( 'input:radio' ).removeAttr( 'disabled' );			
			return;
		}
		
		if ( $( '#package option:selected ' ).val() == '' || $( '#package option:selected ' ).val() == 2 ) {
			disable_radio_buttons();			
			return;
		}
	} );
	
	// Address copying feature
/*	$( '#contact_information :input, #contact_information :select' ).keyup( function() {
		$( '#billing_' + $( this ).attr( 'id' ) ).attr( 'value', $( this ).val() );
	} );	*/
	
	// Approve
	$( 'a.approve' ).click( function( e ) {
		e.preventDefault();
		
		var prompt = confirm( 'Are you sure you wish to approve this person? ');
		
		if ( prompt ) {
			window.location.href = $( this ).attr( 'href' );
		}
	} );
	
	// Deny
	$( 'a.deny' ).click( function( e ) {
		e.preventDefault();
		
		var prompt = confirm( 'Are you sure you wish to deny this person? ');
		
		if ( prompt ) {
			window.location.href = $( this ).attr( 'href' );
		}
	} );

	// Remove
	$( 'a.remove' ).click( function( e ) {
		e.preventDefault();
		
		var prompt = confirm( 'Are you sure you wish to remove this person? ');
		
		if ( prompt ) {
			window.location.href = $( this ).attr( 'href' );
		}
	} );

	// Invite
	$( 'a.invite' ).click( function( e ) {
		e.preventDefault();
		
		var prompt = confirm( 'Do you wish to send an invitation to this person? ');
		
		if ( prompt ) {
			window.location.href = $( this ).attr( 'href' );
		}
	} );
	
	// Custom Price
	/*$( 'a.custom' ).click( function( e ) {
		e.preventDefault();
		
		/*var prompt = confirm( 'Do you wish to send an invitation to this person? ');
		
		if ( prompt ) {
			window.location.href = $( this ).attr( 'href' );
		}
		document.getElementById("customprice").innerHTML='Package Price : <input type="text" name="text">&nbsp;<input type="button" value="Ok" name="Button">';
	} );*/
} );
function addcustomprice(cid){
	document.getElementById("customprice"+cid).innerHTML='Package Price : <input type="text" name="txtCustomPrice'+cid+'" id="txtCustomPrice'+cid+'" class="pricetextbox">&nbsp;<input type="button" value="  Ok  " name="Button" class="okbutton" onclick="makepayment('+cid+');">&nbsp;<input type="button" value="Cancel" name="Button" onclick="hidecustomprice('+cid+');" class="cancelbutton">';
}
function hidecustomprice(cid){
	document.getElementById("customprice"+cid).innerHTML="";
}

function adddeclinedcustomprice(cid){
	document.getElementById("deccustomprice"+cid).innerHTML='Package Price : <input type="text" name="txtCustomPrice'+cid+'" id="txtCustomPrice'+cid+'" class="pricetextbox">&nbsp;<input type="button" value="  Ok  " name="Button" class="okbutton" onclick="makepayment('+cid+');">&nbsp;<input type="button" value="Cancel" name="Button" onclick="hidedeclinedcustomprice('+cid+');" class="cancelbutton">';
}
function hidedeclinedcustomprice(cid){
	document.getElementById("deccustomprice"+cid).innerHTML="";
}
function makepayment(cid){
	var customprice=document.getElementById("txtCustomPrice"+cid).value;
	if(customprice!=""){
		window.location.href="admin/custom/"+cid+"/"+customprice;
	}else{
		alert("Please add a price to continue.");	
	}
}

function showsearch(searchtag){
	var strHTML='<input type="text" name="txtSearch" id="txtSearch"><br><br><input type="button" value="Search" name="Button" class="okbutton" onclick="call_server(\''+searchtag+'\');">&nbsp;<input type="button" value="Cancel" name="Button" onclick="HideSearch();" class="cancelbutton">';
	document.getElementById("searchdiv").innerHTML=strHTML;
	var leftpos=findPosX(document.getElementById(searchtag));
	var toppos=findPosY(document.getElementById(searchtag));
	leftpos=leftpos-170;
	toppos=toppos*1;
	toppos=toppos+20;
	document.getElementById("searchdiv").style.left=leftpos+"px";
	document.getElementById("searchdiv").style.top=toppos+"px";
	document.getElementById("searchdiv").style.display="block";
	document.getElementById("txtSearch").focus();
}

function HideSearch(){
	document.getElementById("searchdiv").style.display="none";
}

function findPosX(obj)	{
	var curleft = 0;
	if(obj.offsetParent)
	while(1){
		curleft += obj.offsetLeft;
		if(!obj.offsetParent)
		break;
		obj = obj.offsetParent;
	}
	else if(obj.x)
	curleft += obj.x;
	return curleft;
}
function findPosY(obj)
{
	var curtop = 0;
	if(obj.offsetParent)
	while(1){
		curtop += obj.offsetTop;
		if(!obj.offsetParent)
		break;
		obj = obj.offsetParent;
	}
	else if(obj.y)
	curtop += obj.y;
	return curtop;
}

// JavaScript Document
function creat_Object()
{ 
	var xmlhttp;
	// This if condition for Firefox and Opera Browsers 
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') 
	{
		try 
		{
			xmlhttp = new XMLHttpRequest();
		} 
		catch (e) 
		{
			alert("Your browser is not supporting XMLHTTPRequest");
			xmlhttp = false;
		}
	}
	// else condition for ie
	else
	{
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	return xmlhttp;
}
		
var request = creat_Object();

function sever_interaction_1()
{
	if(request.readyState == 1)
	{
		document.getElementById("searchdiv").style.display="none";
		document.getElementById(searcheddiv).innerHTML = '<div align="center"><img src="/resources/ajax-loader.gif"></div>';
				
	}
	if(request.readyState == 4)
	{
		var answer = request.responseText;
		//alert(answer);
		document.getElementById(searcheddiv).innerHTML =answer;
		//alert(splitName[8]);
	}
}

function call_server(searchtag)
{
	searchedtag=searchtag;
	searcheddiv=searchedtag+"Result";
	//alert(searchedtag+", "+searcheddiv);
	var d = new Date();
	var curr_msec = d.getMilliseconds();
	var searchBy=document.getElementById("txtSearch").value;
	if(searchBy!=""){
		request.open("GET", "/search-result.php?searchon=" + searchtag + "&searchBy=" + searchBy +  "&msec=" + curr_msec); 
		request.onreadystatechange = sever_interaction_1;
		request.send('');
	}else{
		alert("Search field cannot be blank.");	
	}
}

function manageCms(cmsid){
	winobj=window.open('/manage-cms.php?cmsid='+cmsid,'newwindow','width=700px, height=610px');
	wleft = (screen.width-700) / 2;
  	wtop = (screen.height-610) / 2;
	if (wleft < 0) {
		w = screen.width;
		wleft = 0;
	}
	if (wtop < 0) {
		h = screen.height;
		wtop = 0;
	}
	winobj.moveTo(wleft, wtop);
}
function manageGallery(){
	winobj=window.open('/manage-gallery.php','newwindow1','width=700px, height=610px, scrollbars=1');
	wleft = (screen.width-700) / 2;
  	wtop = (screen.height-610) / 2;
	if (wleft < 0) {
		w = screen.width;
		wleft = 0;
	}
	if (wtop < 0) {
		h = screen.height;
		wtop = 0;
	}
	winobj.moveTo(wleft, wtop);
}
function forgotpassword(){
	winobj=window.open('/forgot-password.php','newwindow','width=400px, height=250px, scrollbars=0');
	wleft = (screen.width-400) / 2;
  	wtop = (screen.height-250) / 2;
	if (wleft < 0) {
		w = screen.width;
		wleft = 0;
	}
	if (wtop < 0) {
		h = screen.height;
		wtop = 0;
	}
	winobj.moveTo(wleft, wtop);
}