(function( $ ){
$.fn.correctLines = function( maxLine, moreText ) {
	return this.each(function( index, self ) {
		var temp, classes, i, lineHeight;

		maxLine = ( temp = /^(?:.*\s)?fixline_([\d]+)(?:\s.*)?$/.exec( self.className ) ) && temp[1] || maxLine || 3;
		moreText = moreText || '...<br />';

		var od = $( self ).css( "display" );
		var clone = $( self ).css( "display", "block" ).clone( true ).
				css( {"height": "auto", "position": "absolute", "width": self.offsetWidth + "px" } ).html("");
		$( self ).css( "display", od ).after( clone );

		var height = 0,
			width = self.offsetWidth,
			lines = 0,
			lastHeight = 0,
			startWord = 0,
			endWord = -1,
			txt = self.innerHTML,
			part = [];

		while( /(<[^>]+) ([^<]+>)/g.exec( txt ) ) {
			txt = txt.replace(/(<[^>]+) ([^<]+>)/, '$1&jftF767Tgjk56&$2');
		}
		txt = txt.split(' ');

		for( i = 0; i < txt.length; i++ ) {

			txt[i] = txt[i].replace(/&jftF767Tgjk56&/g, ' ');
			clone[0].innerHTML += txt[ i ] + ' ';

			if ( clone[0].clientHeight > lastHeight ) {

				height = lastHeight;
				startWord = endWord + 1;
				endWord = i - 1;

				lastHeight = clone[0].offsetHeight;

				if ( ++lines > maxLine ) {
					break;
				}
			}
		}

		if ( lines > maxLine ) {

			$( self ).css({height: height + "px", overflowY: 'hidden'});

			part = txt.slice( startWord, endWord + 1 );
			height = clone.html( part.join(' ') )[0].offsetHeight;
			for( i = part.length - 1; i >= 0; i-- ) {

				clone.html( part.join(' ') + moreText );

				if ( clone[0].offsetHeight > height ) {
					part.splice( i, 1 );
					endWord--;
				} else {
					break;
				}
			}

			txt[ endWord ] += moreText;

			self.innerHTML = txt.join(" ");
		} else {
			$( self ).css({height: "auto"});
		}

		clone.remove();
	});
}
})( jQuery );

function getCursorPosition(ctrl) {
 
	var CaretPos = 0;
	// IE Support
	if (document.selection) {
 
		ctrl.focus ();
		var Sel = document.selection.createRange ();
 
		Sel.moveStart ('character', -ctrl.value.length);
 
		CaretPos = Sel.text.length;
	}
	// Firefox support
	else if (ctrl.selectionStart || ctrl.selectionStart == '0')
		CaretPos = ctrl.selectionStart;
	return (CaretPos);
}
 
function setCursorPosition(ctrl, pos)
{
 
	if(ctrl.setSelectionRange)
	{
		ctrl.focus();
		ctrl.setSelectionRange(pos,pos);
	}
	else if (ctrl.createTextRange) {
		var range = ctrl.createTextRange();
		range.collapse(true);
		range.moveEnd('character', pos);
		range.moveStart('character', pos);
		range.select();
	}
}

function eventMaxLength(areaId, maxChar, counterId, blockEnter)
{
	if (typeof blockEnter == "undefined")
	{
		blockEnter = false;
	}

	jQuery(function(){
		var tCounter = jQuery("#" + counterId);
		var vMaxChar = maxChar;
		var tArea = jQuery("#" + areaId);

		var PressedTab = false;

		function TestValue(){
			if (tArea.val().length > parseInt(vMaxChar)){
				old = getCursorPosition(tArea[0]);
				tArea[0].value = String(tArea[0].value).substr(0, parseInt(vMaxChar));
				setCursorPosition(tArea[0], old);
			}
			if (tCounter) tCounter[0].innerHTML = (parseInt(vMaxChar) - tArea.val().length);
		}

		tArea.keydown(function(b){
			if ((b.ctrlKey)&&(b.which == 13)){
				//AddToSendMsg(tOrder[0].value);
				return false;
			} else if ((b.which == 13)&&(blockEnter)) {
				return false;
			}
		});

		tArea.keypress(function(b){
			if ((b.which == 13)&&(blockEnter)) {
				return false;
			}
			if (b.which !== 17){
				if ((b.ctrlKey)&&(b.which == 67)){
					return true;
				}
				if ((b.which == 9)||((b.keyCode == 9)&&(b.which == 0))){
					tArea[0].value += String.fromCharCode(9);
					PressedTab = true;
				} else if ((b.which !== 8)&&(b.which !== 46)&&
				   (b.which !== 37)&&(b.which !== 38)&&
				   (b.which !== 39)&&(b.which !== 40)&&
				   (b.which !== 33)&&(b.which !== 34)&&
				   (b.which !== 35)&&(b.which !== 36)&&(b.which !== 0)){
					if (tArea.val().length >= parseInt(vMaxChar)){
						if (tCounter) tCounter[0].innerHTML = (parseInt(vMaxChar) - tArea.val().length);
						return false;
					}
				} else {
					TestValue();
				}
			}
			return true;
		});
		tArea.keyup(function(b){
			TestValue();
		});
		tArea.mousemove(function(b){
			TestValue();
		});
		tArea.blur(function(I){
			TestValue();
			if (PressedTab){
				tArea.focus();
				PressedTab = false;
			}
		});

		TestValue();
	});
}

jQuery( document ).ready( function(){

	jQuery(".correct_lines").correctLines( 2 );

	jQuery.fn.correctComment = function() {
		var self = this[ 0 ], temp, classes, i, maxLine = 3;

		if ( self.className.indexOf('fixline_') >= 0 ) {
			classes = self.className.split(" ");
			for( i = 0; i < classes.length; i++ ) {
				if ( classes[ i ].indexOf('fixline_') >= 0) {
					temp = classes[ i ].split("_");
					maxLine = parseInt( temp[1] );
					break;
				}
			}
		}

		if ( window.getComputedStyle ) {
			temp = document.defaultView.getComputedStyle( self , '' );
			curFontSize = temp.fontSize;
			curLineHeight = temp.lineHeight;
		} else if ( self.currentStyle ) {
			curFontSize = self.currentStyle.fontSize;
			curLineHeight = self.currentStyle.lineHeight;
		} else {
			curFontSize = self.style.fontSize;
			curLineHeight = self.style.lineHeight;
		}

		var clone = jQuery( self ).clone( true ).css( {"height": "auto", "width": self.offsetWidth + "px"} ).html("W");

		if ( curLineHeight == "normal" ) {

			jQuery( document.body ).append( clone );

			lineHeight = clone[0].offsetHeight;

			clone.remove();

		} else if ( curLineHeight.indexOf("px") >= 0 ) {
			lineHeight = parseInt( curLineHeight );
		} else {
			lineHeight = parseInt( parseInt( curFontSize ) * curLineHeight );
		}

		var height = lineHeight,
			width = self.offsetWidth,
			lines = 0,
			lastHeight = 0,
			startWorld = 0,
			endWorld = -1,
			txt = self.innerHTML.split(" "),
			more = jQuery( self ).next( ".correct_comment_more" ),
			moreText = more.text();


		clone.html('');

		jQuery( document.body ).append( clone );

		for( i = 0; i < txt.length; i++ ) {

			clone[0].innerHTML += txt[ i ] + ' ';

			if ( clone[0].offsetHeight > lastHeight ) {
				if ( lastHeight == 0 ) {
					height = clone[0].offsetHeight;
				}
				startWorld = endWorld + 1;
				endWorld = i - 1;
				lines++;
				lastHeight = clone[0].offsetHeight;
			}

			if ( lines > maxLine ) {
				break;
			}
		}

		if ( lines > maxLine ) {
			self.style.height = ( lineHeight * maxLine ) + "px";
		} else {
			self.style.height = "auto";
		}

		txt.splice( endWorld + 1, txt.length - endWorld - 1 );
		txt.splice( 0, startWorld );

		for( i = txt.length - 1; i >= 0; i-- ) {

			clone.html( txt.join(' ') + moreText );

			if ( clone[0].offsetHeight > height ) {
				txt.splice( i, 1 );
			} else {
				break;
			}
		}

		clone.css({"display": "inline"}).html( txt.join(' ') );

		width -= clone[0].offsetWidth;

		clone.remove();

		if (self.offsetHeight < self.scrollHeight - 2) {
			more.css({"width": width + "px"}).show();
		} else {
			more.hide();
		}

		jQuery(".content .frame").scRedraw();
	}

	jQuery( ".correct_comment" ).each(function(){
		jQuery( this ).correctComment();
	});

	jQuery( ".top_menu table td a" ).mouseover( function() {

		var dv = jQuery( this ).prev();

		if ( dv.length == 0 ) {
			dv = jQuery( '<div style="position: absolute; width: ' + ( this.offsetWidth ) + 'px; height: ' + ( this.offsetHeight ) + 'px;"></div>' );
			jQuery( this ).before( dv );
		}

		dv.css( {"display": "block", "background-color": "#ee982c", "background-image": "url('/images/_menu_top_bg_over.png')", "border-radius": "10px"} );
		jQuery( this ).css( {"color": "#535353"} );

	}).mouseout( function(){

		var dv = jQuery( this ).prev();

		if ( dv.length > 0 ) {
			dv.css( {"display": "none"} );
			jQuery( this ).css( {"color": "#ffffff"} );
		}

	});

	jQuery(".header_images").each(function(header_index, header_elem)
	{
		jQuery(header_elem).find("td").each(function(index, elem)
		{
			var div = jQuery(this).find("div");
			if (div.css("margin-top") == '-1000px')
			{
				div.css({"margin-top": "0px", "display": "none"});
			} else {
				header_elem.current_index = index;
			}
			if (get('slideshowButton_' + index))
			{
				var sb = jQuery("#slideshowButton_" + index);
				sb[0].button_index = index;
				sb[0].header_elem = header_elem;
				sb.click(function(){
					this.header_elem.stopAnimate(this.button_index);
				});
			}
		});
	});

	jQuery(".header_images").each(function(index, elem)
	{
		var ie6 = ((jQuery.browser.msie) && (parseInt(jQuery.browser.version) == 6)) ? true : false;

		var td = jQuery(elem).find("td");

		var time_to_change = 3000;
		var time_shift = 2000;

		elem.timer_handler = null;
		elem.activeAnimate = true;
		elem.activeButton = false;

		elem.stopAnimate = function(index)
		{
			elem.activeAnimate = false;

			if (elem.timer_handler != null)
			{
				clearTimeout(elem.timer_handler);
				elem.timer_handler = null;
			}

			if (typeof index == "number")
			{
				this.replaceTo(index);
			}
		}

		elem.startAnimate = function()
		{
			if (elem.timer_handler == null)
			{
				elem.activeAnimate = true;
				elem.timer_handler = setTimeout(elem.timer_func, time_to_change);
			}
		}

		elem.replaceTo = function(index)
		{
			if ((elem.current_index != index) && (!elem.activeButton))
			{
				elem.activeButton = true;
				jQuery(td[index]).find("div")[0].elem = elem;
				jQuery(jQuery(td[elem.current_index]).find("div")).fadeOut(time_shift);

				if (get('slideshowButton_' + elem.current_index))
				{
					jQuery("#slideshowButtonActive").animate({
						"left": jQuery('#slideshowButton_' + index)[0].offsetLeft + "px",
						"width": (jQuery('#slideshowButton_' + index)[0].offsetWidth - 9) + "px"
					}, {
						duration: time_shift
					});
				}
				if ((get('slideshowText_' + elem.current_index)) && (get('slideshowText_' + index)))
				{
					jQuery("#slideshowText_" + elem.current_index).fadeOut(time_shift / 2, function(){});
					jQuery("#slideshowText_" + index).fadeIn(time_shift / 2);
				}

				jQuery(jQuery(td[index]).find("div")).fadeIn(time_shift, function(){
					elem.current_index = index;
					elem.activeButton = false;
				});
			}
		}

		if (td.length > 1)
		{
			elem.count_td = td.length;
			elem.timer_func = function()
			{
				if (elem.activeAnimate)
				{
					elem.activeButton = true;
					var next = ((elem.current_index < elem.count_td - 1) ? (elem.current_index + 1) : 0);
					jQuery(td[next]).find("div")[0].elem = elem;
					jQuery(jQuery(td[elem.current_index]).find("div")).fadeOut(time_shift);
					jQuery(jQuery(td[next]).find("div")).fadeIn(time_shift, function(){
						elem.activeButton = false;
						elem.current_index = next;
						this.elem.timer_handler = setTimeout(this.elem.timer_func, time_to_change);
					});
					if (get('slideshowButton_' + elem.current_index))
					{
						jQuery("#slideshowButtonActive").animate({
							"left": jQuery('#slideshowButton_' + next)[0].offsetLeft + "px",
							"width": (jQuery('#slideshowButton_' + next)[0].offsetWidth - 9) + "px"
						}, {
							duration: time_shift
						});
					}
					if ((get('slideshowText_' + elem.current_index)) && (get('slideshowText_' + next)))
					{
						jQuery("#slideshowText_" + elem.current_index).fadeOut(time_shift / 2, function(){});
						jQuery("#slideshowText_" + next).fadeIn(time_shift / 2);
					}
				}
			}
			elem.timer_handler = setTimeout(elem.timer_func, time_to_change);
		}
	});

	jQuery( ".student_type" ).click( function() {
		if ( this.checked ) {
			if ( this.value == 1 ) {
				jQuery(".school_details_block").hide();
				jQuery(".graduation_year_block").hide();
			} else {
				jQuery(".school_details_block").show();
				jQuery(".graduation_year_block").show();
			}
		}
	});

	if (jQuery(".is_hinted").length > 0) {
		setTimeout(function(){
			var ok = true;
			jQuery(".is_hinted").each(function(){
				if ( !this.new_select ) {
					ok = false;
				}
			});

			if ( ok ) {
				jQuery(".is_hinted").each(function(){
					var self = this;
					this.new_select[0].input.blur(function(){
						if( this.value=='' ) {
							jQuery( self ).parent().parent().find('div.is_hint div').show();
						}
					}).focus(function(){
						jQuery( self ).parent().parent().find('div.is_hint div').hide();
					});
				});
			} else {
				setTimeout( arguments.callee, 100 );
			}
		}, 100);
	}

	jQuery(".number_int_only").each(function(){
		jQuery(this).keypress(function(b){
			var C = /[0-9\x25\x27\x24\x23]/;
			var a = b.which;
			var c = String.fromCharCode(a);
			return !!(a==0||a==8||a==9||a==13||c.match(C));
		});
	});

	function getCaretPositionA(elem)
	{
		var CaretPos = 0;
		// IE Support
		if (document.selection)
		{
			//elem.focus ();
			var Sel = document.selection.createRange ();

			Sel.moveStart ('character', -elem.value.length);

			CaretPos = Sel.text.length;
		}
		// Firefox support
		else if (elem.selectionStart || elem.selectionStart == '0')
			CaretPos = elem.selectionStart;
		return (CaretPos);
	}

	jQuery(".price_field").each(function(){

		jQuery(this).keypress(function(b){
			var C= /[0-9\x25\x27\x24\x23\x2E]/;
			var a = b.which;

			var pos = getCaretPositionA(this);

			if ((this.value.indexOf('.') < 0) && (this.value.length == 6)) {
				C= /[\x25\x27\x24\x23\x2E]/;
			}
			if ((this.value.indexOf('.') > 0) || ((pos == 0) && (a == 46)) || ((pos < this.value.length - 2) && (a == 46)))
			{
				C= /[0-9\x25\x27\x24\x23]/;
			}
			if ((this.value.indexOf('.') > 0) && (this.value.length == this.value.indexOf('.') + 3))
			{
				C= /[\x25\x27\x24\x23]/;
			}
			var c = String.fromCharCode(a);
			return !!(a==0||a==8||a==9||a==13||c.match(C));
		}).blur(function(){
			if (this.value.indexOf('.') > 0)
			{
				if (this.value.indexOf('.') == this.value.length - 1)
				{
					this.value += '00';
				} else if (this.value.indexOf('.') == this.value.length - 2) {
					this.value += '0';
				}
			} else if (this.value.length > 0) {
				this.value += '.00';
			}
		});
	});
});

var _last_opened_box;

function closelastbox()
{
	jQuery(_last_opened_box).fadeOut(200, function(){
		if ( this.binded ) {
			jQuery(this).unbind("mouseover");
			this.binded = false;
		}
	});
}

function openbox(id, noBind)
{
	if (jQuery(id)[0].closebutton) {
		return;
	}

	if (jQuery(id)[0]._timeout)
	{
		clearTimeout(jQuery(id)[0]._timeout);
		jQuery(id)[0]._timeout = null;
	}

	if (_last_opened_box && _last_opened_box != id)
	{
		closelastbox();
	}

	if (jQuery(id).css('margin-top') == '-10000px')
	{
		jQuery(id).css({'margin-top': '0px', 'display': 'none'})
	}

	_last_opened_box = id;
	if ( noBind ) {
		jQuery(id).fadeIn(200);
	} else {
		jQuery(id).fadeIn(200, function(){
			if ( !this.binded ) {
				jQuery(this).bind("mouseover", function(){
					openbox('#' + this.id);
				});
				this.binded = true;
			}
		});
	}

	return false;
}

function closebox(id, wait, noOpen)
{
	jQuery(id)[0].closebutton = noOpen;
	if (wait)
	{
		jQuery(id)[0]._timeout = setTimeout("closebox('"+id+"')", 500);
	} else {
		if ( jQuery(id)[0].binded ) {
			jQuery(id).unbind("mouseover");
			jQuery(id)[0].binded = false;
		}
		jQuery(id).fadeOut(200, function(){
			this.closebutton = false;
		});
	}

	return false;
}

function get(id)
{
	return document.getElementById(id);
}

function checkZip(zip, zip_box, state_box, city_box)
{
	jQuery.ajax({
		url: '/ajax/other.php',
		data: "action=checkZip&zip=" + zip,
		type: 'post',
		dataType: 'json',
		zip_box: zip_box,
		state_box: state_box,
		city_box: city_box,
		success: function(data){
			if (data.error == 0)
			{
				if ((this.zip_box) && (this.zip_box.length > 0))
				{
					this.zip_box.empty();
					this.zip_box.append(jQuery('<option></option>'));
					for(var i = 0; i < data.zips.length; i++)
					{
						this.zip_box.append(jQuery('<option value="' + data.zips[i] + '"' + ((data.zips[i] == data.current_zip) ? ' selected="selected"' : '') + '>' + data.zips[i] + '</option>'));
					}
					this.zip_box[0].updateOptions();
				}

				if ((this.state_box) && (this.state_box.length > 0))
				{
					if (this.state_box[0].nodeName == 'SELECT')
					{
						this.state_box.find('option[value="' + data.zip_data.state_name + '"]').attr("selected", "selected");
						this.state_box[0].updateOptions();
					} else if (data.zip_data.state_name) {
						this.state_box[0].value = data.zip_data.state_name;
					}
				}

				if ((this.city_box) && (this.city_box.length > 0))
				{
					if (this.city_box[0].nodeName == 'SELECT')
					{
						this.city_box.empty();
						this.city_box.append(jQuery('<option></option>'));
						for(var i = 0; i < data.citys.length; i++)
						{
							this.city_box.append(jQuery('<option value="' + data.citys[i] + '"' + ((data.citys[i] == data.zip_data.city) ? ' selected="selected"' : '') + '>' + data.citys[i] + '</option>'));
						}
						this.city_box[0].updateOptions();
					} else if (data.zip_data.city) {
						this.city_box[0].value = data.zip_data.city;
					}
				}
			}
		},
		complete: function(xhr){
//			alert(xhr.responseText);
		},
		error: function(xhr, status){
//			alert(xhr.responseText);
		}
	});
}

function changeState(state, zip_box, state_box, city_box)
{
	jQuery.ajax({
		url: '/ajax/other.php',
		data: "action=changeState&state_name=" + state + "&city=" + city_box[0].customVal(),
		type: 'post',
		dataType: 'json',
		zip_box: zip_box,
		state_box: state_box,
		city_box: city_box,
		success: function(data){
			if (data.error == 0)
			{
				if ((this.zip_box) && (this.zip_box.length > 0))
				{
					var t = ((data.zips.length == 1) ? data.zips[0] : this.zip_box[0].customVal());
					this.zip_box.empty();
					this.zip_box.append(jQuery('<option>' + t + '</option>'));
					for(var i = 0; i < data.zips.length; i++)
					{
						this.zip_box.append(jQuery('<option value="' + data.zips[i] + '"' + ((data.zips[i] == t) ? ' selected="selected"' : '') + '>' + data.zips[i] + '</option>'));
					}
					this.zip_box[0].updateOptions();
				}

				if ((this.city_box) && (this.city_box.length > 0))
				{
					var t = this.city_box[0].customVal();

					this.city_box.empty();
					this.city_box.append(jQuery('<option>' + t + '</option>'));
					for(var i = 0; i < data.citys.length; i++)
					{
						this.city_box.append(jQuery('<option value="' + data.citys[i] + '">' + data.citys[i] + '</option>'));
					}
					this.city_box[0].updateOptions();
					this.city_box[0].checkText();
				}
			}
		},
		complete: function(xhr){
//			alert(xhr.responseText);
		},
		error: function(xhr, status){
//			alert(xhr.responseText);
		}
	});
}

function checkCity(city, zip_box, state_box, city_box)
{
	if (city_box[0].request)
	{
		city_box[0].request.abort();
		city_box[0].request = null;
	}

	city_box[0].request = jQuery.ajax({
		url: '/ajax/other.php',
		data: "action=checkCity&city=" + city + "&state_name=" + state_box[0].customVal(),
		type: 'post',
		dataType: 'json',
		zip_box: zip_box,
		state_box: state_box,
		city_box: city_box,
		success: function(data){
			if (data.error == 0)
			{
				var t = this.city_box[0].customVal();

				this.city_box.empty();
				this.city_box.append(jQuery('<option>' + t + '</option>'));
				for(var i = 0; i < data.citys.length; i++)
				{
					this.city_box.append(jQuery('<option value="' + data.citys[i] + '">' + data.citys[i] + '</option>'));
				}
				this.city_box[0].updateOptions();
				this.city_box[0].checkText();
			}
		},
		complete: function(xhr){
			this.city_box[0].request = null;
//			alert(xhr.responseText);
		},
		error: function(xhr, status){
//			alert(xhr.responseText);
		}
	});
}

function changeCity(city, zip_box, state_box, city_box)
{
	jQuery.ajax({
		url: '/ajax/other.php',
		data: "action=changeCity&city=" + city + "&state_name=" + state_box[0].customVal(),
		type: 'post',
		dataType: 'json',
		zip_box: zip_box,
		state_box: state_box,
		city_box: city_box,
		success: function(data){
			if (data.error == 0)
			{
				if ((this.zip_box) && (this.zip_box.length > 0))
				{
					var t = ((data.zips.length == 1) ? data.zips[0] : this.zip_box[0].customVal());
					this.zip_box.empty();
					this.zip_box.append(jQuery('<option>' + t + '</option>'));
					for(var i = 0; i < data.zips.length; i++)
					{
						this.zip_box.append(jQuery('<option value="' + data.zips[i] + '"' + ((data.zips[i] == t) ? ' selected="selected"' : '') + '>' + data.zips[i] + '</option>'));
					}
					this.zip_box[0].updateOptions();
				}

				if ((this.state_box) && (this.state_box.length > 0))
				{
					this.state_box.find('option[value="' + data.current_state + '"]').attr("selected", "selected");
					this.state_box[0].updateOptions();
				}
			}
		},
		complete: function(xhr){
//			alert(xhr.responseText);
		},
		error: function(xhr, status){
//			alert(xhr.responseText);
		}
	});
}

jQuery(document).ready(function()
{
	jQuery(".processCountryBox").each(function()
	{
		var zip = jQuery(this).find(".zip-box");
		var state = jQuery(this).find(".state-box");
		var city = jQuery(this).find(".city-box");

		if (zip.length > 0)
		{
			zip[0].onCustomKeyPress = function(e, elem)
			{
				var allowMatch = /[0-9\x08\x25\x27\x24\x23\x09]/;

				if (elem.value.length >= 5)
				{
					allowMatch = /[\x08\x25\x27\x24\x23\x09]/;
				}

				var a = e.which;
				var c = String.fromCharCode(a);

				if (!c.match(allowMatch))
				{
					return !!(a==0||a==8||a==9||a==13||c.match(allowMatch));
				}

				if ((c.match(/[0-9]/)) && (elem.value.length == 4))
				{
					checkZip(elem.value + c, zip, state, city);
				}
			};
			zip[0].change = function(){
				checkZip(this.customVal(), zip, state, city);
			};
			zip[0].onCustomBlur = function(){
				if (this.customVal().length == 5)
				{
					checkZip(this.customVal(), zip, state, city);
				}
				if (this.custBlur)
				{
					this.custBlur();
				}
			};
		}

		if (state.length > 0 && state[0].nodeName == 'SELECT')
		{
			state[0].change = function(){
				changeState(this.customVal(), zip, state, city);
			};
			state[0].onCustomBlur = function(){
				if (this.custBlur)
				{
					this.custBlur();
				}
			}
		}

		if (city.length > 0 && city[0].nodeName == 'SELECT')
		{
			city[0].onCustomKeyUp = function(e, elem)
			{
				var funcKeyMatch = /[\x0D\x08\x09\x24\x23\x25\x26\x27\x28]/;

				var a = e.which;
				var c = String.fromCharCode(a);

				if ((!c.match(funcKeyMatch)) && ((elem.value.length == 1) || ((!elem.value_checked) && (elem.value.length > 0))))
				{
					elem.value_checked = true;
					checkCity(elem.value.substring(0, 1), zip, state, city);
				} else if (elem.value.length == 0) {
					if (city[0].request)
					{
						city[0].request.abort();
						city[0].request = null;
					}
					elem.value_checked = false;
				}
			};
			city[0].change = function(){
				changeCity(this.customVal(), zip, state, city);
			};
			city[0].onCustomBlur = function(){
				if (this.custBlur)
				{
					this.custBlur();
				}
			};
		}
	});

	jQuery(".processSchoolStudies").each(function() {
		var school_box = jQuery(this).find(".school-box");
		var study_box = jQuery(this).find(".study-box");
		var course_box = jQuery(this).find(".course-box");
		var professor_box = jQuery(this).find(".professor-box");
		var topic_box = jQuery(this).find(".topic-box");
		var execBox = [];

		var data = {
			action: "getAllList"
		}

		var change = function() {
			if ( study_box.length > 0 ) {
				data.study_id = study_box.val();
			}
			if ( school_box.length > 0 ) {
				data.school_id = school_box.val();
			}
			if ( course_box.length > 0 ) {
				data.course_id = course_box.val();
			}
			if ( professor_box.length > 0 ) {
				data.professor_id = professor_box.val();
			}
			if ( topic_box.length > 0 ) {
				data.topic_id = topic_box.val();
			}

			jQuery.ajax({
				url: '/ajax/other.php',
				data: data,
				type: 'post',
				dataType: 'json',
				self: this,
				success: function( data ) {
					if ( data.error == 0 ) {
						var t, i, j, nm, update = false;
						for( i = 0; i < execBox.length; i++ ) {
							if ( !update && execBox[ i ].obj[0] === this.self ) {
								update = true;
								continue;
							}
							if ( update && ( execBox[ i ].obj[0].nodeName == 'SELECT' ) ) {
								t = execBox[ i ].obj.val();
								execBox[ i ].obj.empty();

								if ( execBox[ i ].obj[0].new_select[0].readonly ) {
									execBox[ i ].obj.append(jQuery('<option value="0">Select One</option>'));
								} else {
									execBox[ i ].obj.append(jQuery('<option>' + execBox[ i ].obj[0].customVal() + '</option>'));
								}

								for( j = 0; j < data[ execBox[ i ].objName ].length; j++ ) {
									nm = execBox[ i ][ execBox[ i ].objName ];
									execBox[ i ].obj.append(jQuery('<option value="' + data[ execBox[ i ].objName ][j][ nm + '_id' ] + '"' + ((data[ execBox[ i ].objName ][j][ nm + '_id' ] == t ) ? ' selected="selected"' : '') + '>' + data[ execBox[ i ].objName ][j][ nm + '_name' ] + '</option>'));
								}
								execBox[ i ].obj[0].updateOptions();
							}
						}
					}
				},
				complete: function( xhr ) {
//					alert(xhr.responseText);
				},
				error: function( xhr, status ) {
//					alert(xhr.responseText);
				}
			});
		}

		if ( study_box.length > 0 ) {
			study_box[0].change = change;
			execBox.push({
				studies_data: "study",
				objName: "studies_data",
				obj: study_box
			});
		}
		if ( school_box.length > 0 ) {
			school_box[0].change = change;
			execBox.push({
				schools_data: "school",
				objName: "schools_data",
				obj: school_box
			});
		}
		if ( course_box.length > 0 ) {
			course_box[0].change = change;
			execBox.push({
				courses_data: "course",
				objName: "courses_data",
				obj: course_box
			});
		}
		if ( professor_box.length > 0 ) {
			professor_box[0].change = change;
			execBox.push({
				professors_data: "professor",
				objName: "professors_data",
				obj: professor_box
			});
		}
		if ( topic_box.length > 0 ) {
			topic_box[0].change = change;
			execBox.push({
				topics_data: "topic",
				objName: "topics_data",
				obj: topic_box
			});
		}

	});
});

function openCloseAnswerBox( self, id, func )
{
	var a = jQuery('.answer_box_' + id);
	if (a.css('display') == 'none') {
		a.slideDown(200, function(){
			jQuery( self ).find("span").css({"background-position": "right 0"});
			jQuery( self ).find("span.open").css({"display": "none"});
			jQuery( self ).find("span.close").css({"display": "inline"});
			jQuery( this ).find(".button_div div").scRedraw();
		});
	} else {
		a.slideUp(200, function(){
			jQuery( self ).find("span").css({"background-position": "right -15px"});
			jQuery( self ).find("span.close").css({"display": "none"});
			jQuery( self ).find("span.open").css({"display": "inline"});
			if ( func ) func();
		});
	}
	return false;
}

function openCloseAnswerItBox( self, id )
{
	var a = jQuery('.answer_it_box_' + id);
	if (a.css('display') == 'none') {
		a.slideDown(200, function(){
			jQuery( self ).find("span").css({"background-position": "right 0"});
			jQuery( self ).find("span.open").css({"display": "none"});
			jQuery( self ).find("span.close").css({"display": "inline"});
			jQuery( this ).find(".button_div div").scRedraw();
		});
	} else {
		a.slideUp(200, function(){
			jQuery( self ).find("span").css({"background-position": "right -15px"});
			jQuery( self ).find("span.close").css({"display": "none"});
			jQuery( self ).find("span.open").css({"display": "inline"});
		});
	}
	return false;
}

function openCloseAddTipBox( self )
{
	var a = jQuery('.tips_add_box' );
	if (a.css('display') == 'none') {
		a.slideDown(200, function(){
			jQuery( self ).find("span").css({"background-position": "right 0"});
			jQuery( self ).find("span.open").css({"display": "none"});
			jQuery( self ).find("span.close").css({"display": "inline"});
			jQuery( this ).find(".button_div div").scRedraw();
		});
	} else {
		a.slideUp(200, function(){
			jQuery( self ).find("span").css({"background-position": "right -15px"});
			jQuery( self ).find("span.close").css({"display": "none"});
			jQuery( self ).find("span.open").css({"display": "inline"});
		});
	}
	return false;
}


function submit_answer( self, id )
{
	var fields = jQuery( self.form ).serialize();

	jQuery.ajax({
		url: '/ajax/qa_core.php',
		data: "action=submit_answer&qa_id=" + id + "&" + fields,
		type: 'post',
		dataType: 'json',
		thisButton: self,
		qaId: id,
		success: function(data){

			var el;

			if ( data.error == 1 ) {

				for( var k in data.postErrors ) {
					el = jQuery( this.thisButton.form ).find("[name*=" + k + "]").css( { "background-color": "#FFD8D9" } );

					if ( !el.focusBinded ) {
						jQuery( el ).focus( function() {
							jQuery( this ).css( { "background-color": "#fff" } );
						});
						el.focusBinded = true;
					}
				}
			} else if ( data.error == 0 ) {

				for( var k in data.postFields ) {
					jQuery( this.thisButton.form ).find("[name*=" + k + "]").val( "" ).blur();
				}

				if ( this.thisButton.form.className == "answer_it_form" ) {

					openCloseAnswerItBox( jQuery( ".openclose_button_" + this.qaId )[ 0 ], this.qaId );

					if ( jQuery( ".questionBox" ).length == 1 ) {
						var answer  = jQuery( ".questionBox" ).next().clone();
						answer.attr( "class", "comment_" + data.id );
						answer.find( ".answered" ).html( data.answered );
						answer.find( ".correct_comment" ).html( data.comment );
						if ( data.tags ) {
							answer.find( ".tags" ).html( data.tags );
						} else {
							answer.find( ".tags_box" ).hide();
						}

						answer.find( ".answer_it_now button" ).attr( "class", "openclose_button_" + data.id )[0].onclick = function() {
							return openCloseAnswerBox( this, data.id );
						};

						answer.find( ".reg_button" )[0].onclick = function() {
							return submit_answer( this, data.id );
						};

						answer.find( ".answer_box" )[0].className = "answer_box answer_box_" + data.id;

						var comment = answer.find( ".comments" ).find( ".comment_0" ).clone();

						answer.find( ".comments" ).empty().attr( "class", "comments comments_" + data.id );

						answer.find( ".counters" ).attr( "id", "counter_" + data.id );
						answer.find( ".area" ).val("").attr( "id", "area_" + data.id );
						answer.find( "script" ).remove();

						jQuery( ".questionBox" ).after( answer );

						var rmb = answer.find( ".remove_button" );
						if ( rmb.length > 0 ) {
							rmb[ 0 ].onclick = null;
							rmb.attr( "qaId", data.id ).css("display", "block").click(function(){
								remove_comment( this, jQuery( this ).attr( "qaId" ) );
							});
						}

						answer.find( ".comments" ).append( comment );

						answer.find( ".correct_comment" ).correctComment();
						eventMaxLength('area_' + data.id, 5000, 'counter_' + data.id);
					}
				} else if ( this.thisButton.form.className == "answer_new_form" ) {
					var qaId = this.qaId;
					openCloseAnswerBox( jQuery( ".openclose_button_" + this.qaId )[ 0 ], this.qaId, function(){
						jQuery( ".question_box_" + qaId ).remove();
					} );
				} else {
					openCloseAnswerBox( jQuery( ".openclose_button_" + this.qaId )[ 0 ], this.qaId );
					var comment = jQuery( ".comments_" + this.qaId ).find(".comment_0").clone();
					comment.attr("class", "comment_" + data.id ).css({"display": "block"});
					comment.find(".comment_by").html( "Comment by: " + data.name );
					comment.find(".correct_comment").html( data.comment );

					jQuery( ".comments_" + this.qaId ).append( comment );
					comment.find( ".correct_comment" ).correctComment();
					comment.find( ".remove_button" ).attr( "qaId", data.id ).click(function(){
						remove_comment( this, jQuery( this ).attr( "qaId" ) );
					});
				}
			}
		},
		complete: function(xhr){
			//alert(xhr.responseText);
		},
		error: function(xhr, status){
			//alert(status);
		}
	});

	return false;
}

function remove_comment( self, id, redir )
{
	jQuery.ajax({
		url: '/ajax/qa_core.php',
		data: "action=removeComment&qa_id=" + id,
		type: 'post',
		dataType: 'json',
		thisButton: self,
		qaId: id,
		redir: redir,
		success: function(data){
			if (data.error == 0)
			{
				for( var key in data.items ) {
					jQuery( ".comment_" + data.items[ key ] ).remove();
				}
				if ( this.redir ) {
					document.location = this.redir;
				}
			}
		},
		complete: function(xhr){
			//alert(xhr.responseText);
		},
		error: function(xhr, status){
			//alert(status);
		}
	});

	return false;
}

function set_vote( self, id, type )
{
	jQuery.ajax({
		url: '/ajax/qa_core.php',
		data: "action=setVote&qa_id=" + id + "&vote_type=" + type,
		type: 'post',
		dataType: 'json',
		thisButton: self,
		qaId: id,
		vote_type: type,
		success: function(data){
			if (data.error == 0)
			{
				if (data.vote_type == 1) {
					this.thisButton.innerHTML = '(' + data.p_votes + ')';
				} else {
					this.thisButton.innerHTML = '(' + data.n_votes + ')';
				}
			}
		},
		complete: function(xhr){
			//alert(xhr.responseText);
		},
		error: function(xhr, status){
			//alert(status);
		}
	});

	return false;
}

function set_unset_follow( self, id )
{
	jQuery.ajax({
		url: '/ajax/qa_core.php',
		data: "action=set_unset_follow&qa_id=" + id,
		type: 'post',
		dataType: 'json',
		thisButton: self,
		qaId: id,
		success: function(data){
			if (data.error == 0)
			{
				if (data.type == 1) {
					jQuery(this.thisButton).find(".start").hide();
					jQuery(this.thisButton).find(".stop").show();
				} else {
					jQuery(this.thisButton).find(".stop").hide();
					jQuery(this.thisButton).find(".start").show();
				}
			}
		},
		complete: function(xhr){
			//alert(xhr.responseText);
		},
		error: function(xhr, status){
			//alert(status);
		}
	});

	return false;
}

function sel_star( sub_id, count, cur )
{
	for( var i = 1; i <= cur; i++ )
	{
		document.getElementById(sub_id + '_' + i).style.backgroundPosition = '0px -20px';
	}
	for( var i = count; i > cur; i-- )
	{
		document.getElementById(sub_id + '_' + i).style.backgroundPosition = '0px 0px';
	}
}

function out_star( sub_id, count )
{
	sel_star( sub_id, count, document.getElementById(sub_id).value );
}

function set_star( sub_id, cur )
{
	if (document.getElementById(sub_id).value == cur) cur--;
	document.getElementById(sub_id).value = cur;
}

function buy_more_credit( self, uri )
{
	jQuery( self.form ).find( ".error_field" ).css( "display", "none" );

	var credits = jQuery( self.form ).find(".num_of_credits").val();

	if ( credits.length == 0 || credits.replace( /[^0-9]/g, '' ) !== credits || parseInt( credits ) === 0 ) {
		jQuery( self.form ).find( ".error_field" ).css( "display", "inline" );
	} else if ( jQuery( self.form ).find( ".buy_label" ).css( "display" ) !== "none" ) {

		jQuery( self.form ).find( ".buy_label" ).hide();
		jQuery( self.form ).find( ".sending_label" ).show();

		jQuery.ajax({
			url: '/ajax/order.php',
			data: {
				"action": "buy_more_credit",
				"credits": credits,
				"uri": uri
			},
			type: 'post',
			dataType: 'json',
			thisButton: self,
			credits: credits,
			retStatus: 0,
			success: function( data ) {
				if ( data.error == 0 )
				{
					this.retStatus = data.status;

					if ( data.status == 1 ) {
						for( var key in data.fields ) {
							jQuery( this.thisButton.form ).append( '<input type="hidden" name="' + key + '" value="' + data.fields[ key ] + '" />' );
						}
						this.thisButton.form.submit();
					} else {
					}
				}
			},
			complete: function(xhr){
				if ( this.retStatus != 1 ) {
					jQuery( this.thisButton.form ).find( ".sending_label" ).hide();
					jQuery( this.thisButton.form ).find( ".buy_label" ).show();
				}
				//alert(xhr.responseText);
			},
			error: function(xhr, status){
				//alert(status);
			}
		});
	}

	return false;
}

function redeem_credit( self, nums )
{
	nums = nums || '';

	if ( jQuery( self.form ).find( ".buy_label" ).css( "display" ) !== "none" ) {

		jQuery( self.form ).find( ".buy_label" ).hide();
		jQuery( self.form ).find( ".sending_label" ).show();

		jQuery( self.form ).find( ".lbl" ).each( function(){
			this.innerHTML = '&nbsp;';
		});

		var params = jQuery( self.form ).serialize();

		jQuery.ajax({
			url: '/ajax/order.php',
			data: params,
			type: 'post',
			dataType: 'json',
			thisButton: self,
			retStatus: 0,
			success: function( data ) {
				if ( data.error == 0 )
				{
					var error = false;
					for( var field in data.postErrors ) {
//alert( field );
						jQuery( this.thisButton.form ).find( "." + field + "_error" ).html( data.postErrors[ field ] );
						error = true;
					}

					if ( !error ) {
						openbox( '#redeem_credits_box_success' + nums, true );
					}
				}
			},
			complete: function(xhr){
				jQuery( this.thisButton.form ).find( ".sending_label" ).hide();
				jQuery( this.thisButton.form ).find( ".buy_label" ).show();
				//alert(xhr.responseText);
			},
			error: function(xhr, status){
				//alert(status);
			}
		});
	}

	return false;
}

function deleteDownloadItem( id ) {

	jQuery.ajax({
		url: '/ajax/other.php',
		data: 'action=deleteDownloadItem&id=' + id,
		type: 'post',
		dataType: 'json',
		thisId: id,
		success: function( data ) {
			if ( data.error == 0 ) {
				jQuery("#down_item_" + this.thisId ).remove();
				jQuery("#total_down_item").html( data.total );
			}
		},
		complete: function( xhr ) {
			//alert(xhr.responseText);
		},
		error: function( xhr, status ) {
			//alert(status);
		}
	});

	return false;
}

function sendRequestToDelOutline( self, id ) {
	
	jQuery.ajax({
		url: '/ajax/other.php',
		data: 'action=sendRequestToDelOutline&id=' + id,
		type: 'post',
		dataType: 'json',
		thisId: id,
		success: function( data ) {
			if ( data.error == 0 ) {
				closebox('#send_request_to_del_' + this.thisId, false, true);
			}
		},
		complete: function( xhr ) {
			//alert(xhr.responseText);
		},
		error: function( xhr, status ) {
			//alert(status);
		}
	});

	return false;
}

function sendRequestToDelQA( self, id ) {

	jQuery.ajax({
		url: '/ajax/other.php',
		data: 'action=sendRequestToDelQA&id=' + id,
		type: 'post',
		dataType: 'json',
		thisId: id,
		success: function( data ) {
			if ( data.error == 0 ) {
				closebox('#send_request_to_del_' + this.thisId, false, true);
			}
		},
		complete: function( xhr ) {
			//alert(xhr.responseText);
		},
		error: function( xhr, status ) {
			//alert(status);
		}
	});

	return false;
}


