// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults 

function loading(obj1, obj2) {
    $(obj1).style.display = '';
    $(obj2).innerHTML = '';
}

function loaded(obj1) {
    $(obj1).style.display = 'none';
}

function setSearch(hwnd) {
	if ($(hwnd).value == "1") {
		$("src_item").style.display = "inline";
		$("src_user").style.display = "none";
		$("src_tt").style.display = "none";
		$("search_mode").value = "1"
	} else if ($(hwnd).value == "2") {
		$("src_item").style.display = "none";
		$("src_user").style.display = "inline";
		$("src_tt").style.display = "none";
		$("search_mode").value = "2"
	} else if ($(hwnd).value == "3") {
		$("src_item").style.display = "none";
		$("src_user").style.display = "none";
		$("src_tt").style.display = "inline";
		$("search_mode").value = "3"
	}
}

function setState() {
    if ($("user_country_id").selectedIndex == 184) {
        $("user_state_id").style.display = '';
        $("user_state_id").selectedIndex = 0;
        $("user_other_state").style.display = 'none';
    } else {
        $("user_state_id").style.display = 'none';
        $("user_other_state").style.display = '';
        $("user_other_state").value = '';
    }
}

/* ------------- ------------------- */
function toggle_country() {
    var cn = $("country_name").value;
    //alert(cn);
    $("user_country").value = cn;
    $("user_state").value = '';
    if (cn == "United States") {
        $("other_country").style.display = 'none';
        $("other_country").value = '';
        $("state_name").style.display = '';
        $("other_state").style.display = 'none';
        $("other_state").value = '';
    } else if (cn == "Others") {
        //$("country_name").style.display = 'none';
        $("other_country").style.display = '';
        $("other_country").focus();
        $("state_name").style.display = 'none';
        $("other_state").style.display = '';
    } else {
        $("other_country").style.display = 'none';
        $("state_name").style.display = 'none';
        $("other_state").style.display = '';
    }
}

function set_country() {
    if ($("country_name").value == "Others") {
        $("user_country").value = $("other_country").value;
        $("other_state").style.display = '';
        //alert($("other_country").value);
    }
}

function set_state() {
    if ($("country_name").value == "United States") {
        $("user_state").value = $("state_name").value;
    } else {
        $("user_state").value = $("other_state").value;
    }
    //alert($("user_state").value);
}

function hide_other_state() {
    $("user_other_state").style.display = 'none';
}

function hideDetails() {
        $("dvd_details").style.display = 'none';
        $("vcd_details").style.display = 'none';
        $("cd_details").style.display = 'none';
        $("book_details").style.display = 'none';
        $("game_details").style.display = 'none';
        $("vhs_details").style.display = 'none';
        $("service_details").style.display = 'none';
    }
    
function showDetails() {
    //hideDetails();
    var opt = $F("items_category_id");
    hideDetails();    
    switch(opt) {
        case "1":
            $("dvd_details").style.display = '';
            break;
            
        case "2":
            $("vcd_details").style.display = '';
            break;
            
        case "3":
            $("cd_details").style.display = '';
            break;
            
        case "4":
            $("book_details").style.display = '';
            break;
            
        case "5":
            $("game_details").style.display = '';
            break;
            
        case "6":
            $("vhs_details").style.display = '';
            break;
            
        case "7":
            $("service_details").style.display = '';
            break;
    }
}

/* AJAX call for forgot password */
function get_pwd() {
	new Ajax.Updater({
		failure:'pwd_error'
	}, 
	
	'/index/process_forget_password', 
	
	{
		asynchronous:true, 
		evalScripts:true, 
		
		onLoaded:function(request){
			Element.hide('imgForgot')
		}, 
		
		onLoading:function(request){
			Element.show('imgForgot')
		},
		
		onSuccess:function(request){
			fr_pwd_dlg.hide();
			alert('Your passowrd has been emailed to you.')
		},
		
		parameters:Form.serialize('frm_fr_pwd')
	});
}

/* AJAX call for sending item request */
function send_request() {
	new Ajax.Updater({
		failure:'itm_error'
	},
	
	'/items/process_item_request',
	
	{
		asynchronous:true, 
		evalScripts:true, 
		
		onLoaded:function(request){
			Element.hide('imgRequest')
		}, 
		
		onLoading:function(request){
			Element.show('imgRequest')
		},
		
		onSuccess:function(request){
			alert(request.responseText)
			itm_req_dlg.hide();
		},

		onFailure:function(request){
		    alert("Sorry, your request cannot be completed. Make sure your Address (under MyAccount) is not empty, and requested an item in your college.");
		},
		
		parameters:Form.serialize('frm_request')
	});
}

/* AJAX call for replying to personal message */
function reply_to_pm() {
	new Ajax.Updater({
		success:'success_pm_reply',
		failure:'failed_pm_reply'
	}, 
	
	'/myaccount/mymessages/process_reply', 
	
	{
		asynchronous:true, 
		evalScripts:true, 
		
		onLoaded:function(request){
			Element.hide('imgPMsg')
		}, 
		
		onLoading:function(request){
			Element.show('imgPMsg')
		},
		
		onSuccess:function(request){
			pm_msg_dlg.hide();
		},
		
		parameters:Form.serialize('frm_pm_reply')
	});
}

/* AJAX call for sending PM */
function send_pm() {
	new Ajax.Updater({
	    success:'success_send_pm',
		failure:'pm_error'
	},
	
	'/member/profile/process_contact',
	
	{
		asynchronous:true, 
		evalScripts:true, 
		
		onLoaded:function(request){
			Element.hide('imgPm')
		}, 
		
		onLoading:function(request){
			Element.show('imgPm')
		},
		
		onSuccess:function(request){
		/*	alert(request.responseText) */
			contact_dlg.hide();
		},

		onFailure:function(request){
		    alert("Sorry, your PM cannot be sent out. Make sure subject & body are not both empty.");
		},
		
		parameters:Form.serialize('frm_contact')
	});
}

/* AJAX call for posting public message */
function post_writing() {
	new Ajax.Updater({
	    success:'success_post_writing',
		failure:'pm_error'
	},
	
	'/member/profile/post_public_msg',
	
	{
		asynchronous:true, 
		evalScripts:true, 
		
		onLoaded:function(request){
			Element.hide('imgPm')
		}, 
		
		onLoading:function(request){
			Element.show('imgPm')
		},
		
		onSuccess:function(request){
		/*	alert(request.responseText) */
			postpm_dlg.hide();
		},

		onFailure:function(request){
		    alert("Sorry, your writing cannot be posted. Make sure writing is not empty.");
		},
		
		parameters:Form.serialize('frm_pubmsg')
	});
}


/* AJAX call for inviting new member */
function invite() {
	new Ajax.Updater({
	    success:'invite_success',
		failure:'invite_error'
	},
	
	'/circles/detail/process_invite_new',
	
	{
		asynchronous:true, 
		evalScripts:true, 
		
		onLoaded:function(request){
			Element.hide('imgInvite')
		}, 
		
		onLoading:function(request){
			Element.show('imgInvite')
		},
		
		onSuccess:function(request){
		/*	alert(request.responseText) */
			invite_dlg.hide();
		},
		
		onFailure:function(request){
		    alert("Please make sure the email address is correct. In addition, you can only invite people with college-affiliated emails.");
		},
		
		parameters:Form.serialize('frm_invite')
	});
}

function broadcast() {
	new Ajax.Updater({
	    success:'broadcast_success',
		failure:'broadcast_error'
	},
	
	'/circles/detail/process_broadcast',
	
	{
		asynchronous:true, 
		evalScripts:true, 
		
		onLoaded:function(request){
			Element.hide('imgBc')
		}, 
		
		onLoading:function(request){
			Element.show('imgBc')
		},
		
		onSuccess:function(request){
		/*	alert(request.responseText) */
			broadcast_dlg.hide();
		},
		
		onFailure:function(request){
		    alert("Oops, there is an error, but your message may be emailed to each member anyway (including yourself). Check your own email.");
		},
		
		parameters:Form.serialize('frm_broadcast')
	});
}

/* AJAX call for notice of inviting fb friends */
function invite_fbfrnd() {
	new Ajax.Updater({
	    success:'invite_success',
		failure:'invite_error'
	},
	
	'/fb/post_note_fb_inv',
	
	{
		asynchronous:true, 
		evalScripts:true, 
		
		onLoaded:function(request){
			Element.hide('imgInvite')
		}, 
		
		onLoading:function(request){
			Element.show('imgInvite')
		},
		
		onSuccess:function(request){
		/*	alert(request.responseText) */
			note_fb_inv_dlg.hide();
		},
		
		onFailure:function(request){
		/*    alert("Please make sure the email address is correct. In addition, you can only invite people with college-affiliated emails."); */
		},
		
		parameters:Form.serialize('frm_note_fb_inv')
	});
}


/* yui-ext starts */
var tt_msg_dlg, pm_msg_dlg, site_info_dlg, fr_pwd_dlg, rc_feed_dlg, itm_req_dlg, contact_dlg, invite_dlg, postpm_dlg, broadcast_dlg, note_fb_inv_dlg;
var pm_btn, pm_tabs, site_info_tabs;

// Creates a basic dialog for forgot passowrd
function forgot_passowrd(dlg, hwnd) {
	if(!fr_pwd_dlg){ // lazy initialize the dialog and only create it once
		fr_pwd_dlg = new YAHOO.ext.BasicDialog(dlg, {
			//modal:true,
			autoTabs:true,
			autoScroll: true,
			resizable: true,
			width:330,
			height:205,
			shadow:true,
			fixedcenter:true,
			shim:true,
			proxyDrag: true
		});
		fr_pwd_dlg.addKeyListener(27, fr_pwd_dlg.hide, fr_pwd_dlg);
		fr_pwd_dlg.addButton('Close', fr_pwd_dlg.hide, fr_pwd_dlg);
		fr_pwd_dlg.addButton('Submit', get_pwd, fr_pwd_dlg);
		
		// Clear error message and reset the form
		fr_pwd_dlg.on('hide', function(){
			$("pwd_error").innerHTML = '';
			$("frm_fr_pwd").reset();
		});
	}
	fr_pwd_dlg.show(hwnd);
}

// Creates a basic dialog for item request
function item_request(dlg, hwnd) {
	/* to fix firefox cursor issue */
	var useShim;
	var ua = navigator.userAgent.toLowerCase();
	if (ua.indexOf("msie") > -1){
		useShim = true;
	}else{
		useShim = false;
	}
	
	if(!itm_req_dlg){ // lazy initialize the dialog and only create it once
		itm_req_dlg = new YAHOO.ext.BasicDialog(dlg, {
			//modal:true,
			autoTabs:true,
			autoScroll: true,
			resizable: true,
			width:500,
			height:350,
			shadow:true,
			fixedcenter:true,
			shim:useShim,
			proxyDrag: true
		});
		itm_req_dlg.addKeyListener(27, itm_req_dlg.hide, itm_req_dlg);
		itm_req_dlg.addButton('Close', itm_req_dlg.hide, itm_req_dlg);
		itm_req_dlg.addButton('Submit', send_request, itm_req_dlg);
		
		// Clear error message and reset the form
		itm_req_dlg.on('hide', function(){
			$("itm_error").innerHTML = '';
			$("frm_request").reset();
		});
	}
	itm_req_dlg.show(hwnd);
}

// Creates a basic dialog for viewing ttcircle messages
function show_tt_msg(dlg, hwnd) {
	if(!tt_msg_dlg){ // lazy initialize the dialog and only create it once
		tt_msg_dlg = new YAHOO.ext.BasicDialog(dlg, {
			//modal:true,
			autoTabs:true,
			autoScroll: true,
			resizable: true,
			width:500,
			height:350,
			shadow:true,
			minWidth:300,
			minHeight:250,
			shim:true,
			proxyDrag: true
		});
		tt_msg_dlg.addKeyListener(27, tt_msg_dlg.hide, tt_msg_dlg);
		tt_msg_dlg.addButton('Close', tt_msg_dlg.hide, tt_msg_dlg);
	}
	tt_msg_dlg.show(hwnd);
}

// Creates a basic dialog for viewing ttcircle messages
function show_pm_msg(dlg, hwnd) {

	/* to fix firefox cursor issue */
	var useShim;
	var ua = navigator.userAgent.toLowerCase();
	if (ua.indexOf("msie") > -1){
		useShim = true;
	}else{
		useShim = false;
	}
	
	if(!pm_msg_dlg){ // lazy initialize the dialog and only create it once
		pm_msg_dlg = new YAHOO.ext.BasicDialog(dlg, {
			//modal:true,
			autoTabs:true,
			autoScroll: true,
			resizable: true,
			width:500,
			height:350,
			shadow:true,
			minWidth:300,
			minHeight:250,
			shim:useShim,
			proxyDrag: true
		});
		pm_msg_dlg.addKeyListener(27, pm_msg_dlg.hide, pm_msg_dlg);
		pm_msg_dlg.addButton('Close', pm_msg_dlg.hide, pm_msg_dlg);
		pm_btn = pm_msg_dlg.addButton('Submit', reply_to_pm, pm_msg_dlg);
		
		// store a refeence to the tabs
		pm_tabs = pm_msg_dlg.getTabs();
		
		// hide the post button if not on Post tab
		pm_tabs.on('tabchange', function(panel, tab){
			pm_btn.setVisible(tab.id == 'pm_reply_msg');
			// Get the id of the sender
			$("personal_message_receiver_id").value = $("pm_sender_id").value;
			//to be restored later// $("personal_message_base_id").value = $("pm_base_id").value;
			
			if ($("personal_message_message").value=='') {
            $("personal_message_message").value = "\n------------------------------------- Original Message -------------------------------------\n"+$("pm_content").value;
            };
		});
		
		// Clear error message and reset the form
		pm_msg_dlg.on('hide', function(){
			$("failed_pm_reply").innerHTML = '';
			$("frm_pm_reply").reset();
		});
        /*
 		pm_msg_dlg.on('beforeshow', function(){
 			getEl(dlg).appendTo(document.body);
 		});
        */
		// Clear success message, and add "Re: ...." to subject line when replying
		pm_msg_dlg.on('show', function(){
			$("success_pm_reply").innerHTML = '';
			//$("personal_message_subject").value = "Re: " + hwnd.innerHTML;
			document.location.href="#msg";			
		});
	}	
	
	pm_tabs.activate('pm_view_msg');
	pm_btn.setVisible(false);
	pm_msg_dlg.show(hwnd);
	$("personal_message_subject").value = "Re: " + hwnd.innerHTML;
}

// Create a basic dialog for site info
function show_site_info(hwnd, tabPage) {
	
	if(!site_info_dlg){ // lazy initialize the dialog and only create it once
		site_info_dlg = new YAHOO.ext.BasicDialog("site_info", {
			//modal:true,  //Wendong: should be true?
			autoTabs:true,
			autoScroll: true,
			resizable: true,
			width:450,
			height:350,
			shadow:true,
			minWidth:300,
			minHeight:250,
			shim:true,
			proxyDrag: true
		});
		site_info_dlg.addKeyListener(27, site_info_dlg.hide, site_info_dlg);
		site_info_dlg.addButton('Close', site_info_dlg.hide, site_info_dlg).focus();
		
		// store a refeence to the tabs
		site_info_tabs = site_info_dlg.getTabs();
		
	}	
	
	site_info_tabs.activate(tabPage);
	site_info_dlg.show(hwnd);
}

// Creates a basic dialog for viewing received feedback
function show_rc_feed(dlg, hwnd) {
	if(!rc_feed_dlg){ // lazy initialize the dialog and only create it once
		rc_feed_dlg = new YAHOO.ext.BasicDialog(dlg, {
			modal:true,
			autoTabs:true,
			autoScroll: true,
			resizable: true,
			width:500,
			height:350,
			shadow:true,
			minWidth:300,
			minHeight:250,
			shim:true,
			proxyDrag: true
		});
		rc_feed_dlg.addKeyListener(27, rc_feed_dlg.hide, rc_feed_dlg);
		rc_feed_dlg.addButton('Close', rc_feed_dlg.hide, rc_feed_dlg);
	}
	rc_feed_dlg.show(hwnd);
}

// Creates a basic dialog for sending PM
function contact(dlg, hwnd) {
	/* to fix firefox cursor issue */
	var useShim;
	var ua = navigator.userAgent.toLowerCase();
	if (ua.indexOf("msie") > -1){
		useShim = true;
	}else{
		useShim = false;
	}
	
	if(!contact_dlg){ // lazy initialize the dialog and only create it once
		contact_dlg = new YAHOO.ext.BasicDialog(dlg, {
			//modal:true,
			autoTabs:true,
			autoScroll: true,
			resizable: true,
			width:500,
			height:350,
			shadow:true,
			fixedcenter:true,
			shim:useShim,
			proxyDrag: true
		});
		contact_dlg.addKeyListener(27, contact_dlg.hide, contact_dlg);
		contact_dlg.addButton('Close', contact_dlg.hide, contact_dlg);
		contact_dlg.addButton('Send', send_pm, contact_dlg);
		
		// Clear error message and reset the form
		contact_dlg.on('hide', function(){
			$("pm_error").innerHTML = '';
			$("frm_contact").reset();
		});
	}
	contact_dlg.show(hwnd);
}

// Creates a basic dialog for sending invitation
function send_invitation(dlg, hwnd) {
	/* to fix firefox cursor issue */
	var useShim;
	var ua = navigator.userAgent.toLowerCase();
	if (ua.indexOf("msie") > -1){
		useShim = true;
	}else{
		useShim = false;
	}
	
	if(!invite_dlg){ // lazy initialize the dialog and only create it once
		invite_dlg = new YAHOO.ext.BasicDialog(dlg, {
			//modal:true,
			autoTabs:true,
			autoScroll: true,
			resizable: true,
			width:510,
			height:490,
			shadow:true,
			fixedcenter:true,
			shim:useShim,
			proxyDrag: true
		});
		invite_dlg.addKeyListener(27, invite_dlg.hide, invite_dlg);
		invite_dlg.addButton('Close', invite_dlg.hide, invite_dlg);
		invite_dlg.addButton('Submit', invite, invite_dlg);
		
		// Clear error message and reset the form
		invite_dlg.on('hide', function(){
			$("invite_error").innerHTML = '';
			$("frm_invite").reset();
		});
	}
	invite_dlg.show(hwnd);
}

// Creates a basic dialog for broacasting
function send_broadcast(dlg, hwnd) {
	/* to fix firefox cursor issue */
	var useShim;
	var ua = navigator.userAgent.toLowerCase();
	if (ua.indexOf("msie") > -1){
		useShim = true;
	}else{
		useShim = false;
	}
	
	if(!broadcast_dlg){ // lazy initialize the dialog and only create it once
		broadcast_dlg = new YAHOO.ext.BasicDialog(dlg, {
			//modal:true,
			autoTabs:true,
			autoScroll: true,
			resizable: true,
			width:510,
			height:490,
			shadow:true,
			fixedcenter:true,
			shim:useShim,
			proxyDrag: true
		});
		broadcast_dlg.addKeyListener(27, broadcast_dlg.hide, broadcast_dlg);
		broadcast_dlg.addButton('Close', broadcast_dlg.hide, broadcast_dlg);
		broadcast_dlg.addButton('Send', broadcast, broadcast_dlg);
		
		// Clear error message and reset the form
		broadcast_dlg.on('hide', function(){
			$("broadcast_error").innerHTML = '';
			$("frm_broadcast").reset();
		});
	}
	broadcast_dlg.show(hwnd);
}

// Creates a basic dialog for posting public msg
function postpm(dlg, hwnd) {
	/* to fix firefox cursor issue */
	var useShim;
	var ua = navigator.userAgent.toLowerCase();
	if (ua.indexOf("msie") > -1){
		useShim = true;
	}else{
		useShim = false;
	}
	
	if(!postpm_dlg){ // lazy initialize the dialog and only create it once
		postpm_dlg = new YAHOO.ext.BasicDialog(dlg, {
			//modal:true,
			autoTabs:true,
			autoScroll: true,
			resizable: true,
			width:500,
			height:350,
			shadow:true,
			fixedcenter:true,
			shim:useShim,
			proxyDrag: true
		});
		postpm_dlg.addKeyListener(27, postpm_dlg.hide, postpm_dlg);
		postpm_dlg.addButton('Close', postpm_dlg.hide, postpm_dlg);
		postpm_dlg.addButton('Post', post_writing, postpm_dlg);
		
		// Clear error message and reset the form
		postpm_dlg.on('hide', function(){
			$("pm_error").innerHTML = '';
			$("frm_pubmsg").reset();
		});
	}
	postpm_dlg.show(hwnd);
}

// Creates a basic dialog for sending fb invitation
function send_fb_inv(dlg, hwnd) {
	/* to fix firefox cursor issue */
	var useShim;
	var ua = navigator.userAgent.toLowerCase();
	if (ua.indexOf("msie") > -1){
		useShim = true;
	}else{
		useShim = false;
	}
	
	if(!note_fb_inv_dlg){ // lazy initialize the dialog and only create it once
		note_fb_inv_dlg = new YAHOO.ext.BasicDialog(dlg, {
			//modal:true,
			autoTabs:true,
			autoScroll: true,
			resizable: true,
			width:510,
			height:490,
			shadow:true,
			fixedcenter:true,
			shim:useShim,
			proxyDrag: true
		});
		note_fb_inv_dlg.addKeyListener(27, note_fb_inv_dlg.hide, note_fb_inv_dlg);
		note_fb_inv_dlg.addButton('Cancel', note_fb_inv_dlg.hide, note_fb_inv_dlg);
		note_fb_inv_dlg.addButton('OK', invite_fbfrnd, note_fb_inv_dlg);
		
		// Clear error message and reset the form
		note_fb_inv_dlg.on('hide', function(){
			$("invite_error").innerHTML = '';
			$("frm_note_fb_inv").reset();
		});
	}
	note_fb_inv_dlg.show(hwnd);
}

/* yui-ext ends */