// ################################################## //
// ===============JavaScript Document================ //
// The belo functions validate different input fields //
// ========created by santrooper@gmail.com=========== //
// ################################################## //

function jstrim(strToTrim){
	try{
		return strToTrim.replace(/^\s+|\s+$/g,"");
	}
	catch(err){}
}

//#######################################################################//

function getDomain(){
	try{
	   //var rootDir = (jstrim(window.location.hostname).toLowerCase()=='localhost')?'/new_mpt_uk/':'/new_mpt_uk/';
   	   //var rootDir = (jstrim(window.location.hostname).toLowerCase()=='localhost')?'/new_mpt_uk/':'/updateuk/';
	   var rootDir = (jstrim(window.location.hostname).toLowerCase()=='localhost')?'/new_mpt_uk/':'/';
	   var domain  = 'http://'+jstrim(window.location.hostname)+rootDir;
	   return(domain);
	}
	catch(err){}
}

//#######################################################################//

function getCenterDomain(){
	try{
	   var rootDir = (jstrim(window.location.hostname).toLowerCase()=='localhost')?'/new_mpt_thai/tutorialcenters/':'/new_mpt_thai/tutorialcenters/';
   	   //var rootDir = (jstrim(window.location.hostname).toLowerCase()=='localhost')?'/new_mpt/tutorialcenters/':'/tutorialcenters/';
	   var domain  = 'http://'+jstrim(window.location.hostname)+rootDir;
	   return(domain);
	}
	catch(err){}
}


//#######################################################################//

function validEmail(usremail){
	$(document).ready(function(){
		try{
			var emailexpr = new RegExp(/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/);
			usremail=jstrim(usremail);
			rtnval=(emailexpr.test(usremail))?true:false;
			return(rtnval);
		}
		catch(err){}
	});
}

//#######################################################################//

function validText(usrtext){
	$(document).ready(function(){
		try{
			//var textexpr = new RegExp(/!|@|\$|\^|#|\*|&|\%|\+|=|\-|\?|~|`|'|"|;|,|\.|:|\[|\]|\(|\)|\{|\}|<|>|[0-9]|\|/g);
			var textexpr = new RegExp(/!|@|\$|\^|#|\*|&|\%|\+|=|\-|\?|~|`|'|"|;|,|\.|:|\[|\]|\(|\)|\{|\}|<|>|\|/g);
			usrtext=jstrim(usrtext);
			rtnval=(textexpr.test(usrtext))?true:false;
			return(rtnval);
		}
		catch(err){}
	});
}

//#######################################################################//

function validURL(usrurl){
	$(document).ready(function(){
		try{
			var urlexpr = new RegExp(/^(?:http:\/\/)?(?:[\w-]+\.)+[a-z]{2,6}$/i);
			usrurl=jstrim(usrurl);
			rtnval=(urlexpr.test(usrurl))?true:false;
			return(rtnval);
		}
		catch(err){}
	});
}

//#######################################################################//

function isPrintableCharacter(keycode) {
	if((keycode >= 48 && keycode <= 90) ||      // 0-1a-z
	   (keycode >= 96 && keycode <= 111) ||     // numpad 0-9 + - / * .
	   (keycode >= 186 && keycode <= 192) ||    // ; = , - . / ^
	   (keycode >= 219 && keycode <= 222)       // ( \ ) '
	  ){
		  return true;
	  } else {
		  return false;
	  }
}

//#######################################################################//

function validInput(checktype, fieldval){
	$(document).ready(function(){
		try{
			fieldval = jstrim(fieldval);
			
			switch(checktype){
				case 'number':
					chrpool=/^[0-9]+$/;
				break;
				case 'character':
					chrpool=/^[A-Za-z]+$/;
				break;
				case 'alphanumeric':
					chrpool=/^[A-Za-z0-9]+$/;
				break;
				default:
					chrpool=/^[A-Za-z0-9]+$/;
				break;
			}
			
			var trgexpr = new RegExp(chrpool);
			rtnval=(trgexpr.test(fieldval))?true:false;
			return(rtnval);
		}
		catch(err){}
	});
}

//#######################################################################//


function submitForm(formId){
	$(document).ready(function(){
		try{
			$('#'+formId).submit();
		}
		catch(err){
			alert('Form could not be submited');
		}
	});
}

//#######################################################################//

function togolSlideDiv(targetDivId){
	$(document).ready(function(){
		try{
			if($('#'+targetDivId).is(":hidden")){
				$('#'+targetDivId).show(200);
			}
			else{
				$('#'+targetDivId).hide(200);
			}	
		}
		catch(err){}
	});
}

//#######################################################################//

function togolShowHideDiv(targetDivId, mode){
	try{
		trgObj = document.getElementById(targetDivId);
		
		if (mode=='show'){
			trgObj.style.display    = 'block';
			trgObj.style.visibility = 'visible';
		}
		else{
			trgObj.style.display    = 'none';
			trgObj.style.visibility = 'hidden';
		}
	}
	catch(err){}
}

//#######################################################################//

function toggleDiv(targetDivId){
	$(document).ready(function(){
		try{
			$('#'+targetDivId).toggle(400); 
		}
		catch(err){}
	});
}

//#######################################################################//

function returnValueToOpenerWin(targetfld, notifydiv, fldval){ 
	$(document).ready(function(){
		try{
			admBasUrl = getDomain() + 'admin/';
			$('#'+targetfld, window.opener.document).val(fldval);
			$('#'+notifydiv, window.opener.document).html('<img src="'+admBasUrl+'images/right.gif">');
			
			closePopWidow();
		}
		catch(err){}
	});
}

//#######################################################################//

function markUploadStatusDiv(marktype, notifydiv){ 
	$(document).ready(function(){
		try{
			admBasUrl = getDomain() + 'admin/';
			markImg   = (marktype=='markyes')?'images/right.gif':'images/cross.gif';
			$('#'+notifydiv).html('<img src="'+admBasUrl+markImg+'">');
		}
		catch(err){}
	});
}

//#######################################################################//

function changeSearchByDiv(){ 
	$(document).ready(function(){
		try{
			searchbyVal = $('#searchby').val();
			
			if(searchbyVal=='carat'){
				$('#searchbyprice').hide('fast', function() { $('#searchbycarat').show(); });
			}
			else{
				$('#searchbycarat').hide('fast', function() { $('#searchbyprice').show(); });
			}
		}
		catch(err){}
	});
}

//#######################################################################//

function checkClarity(){ 
	$(document).ready(function(){
		try{
			clrFromVal = jstrim($('#fromclarity').val());
			clrToVal   = jstrim($('#toclarity').val());
			
			if(clrFromVal!='' && clrToVal!=''){
				//alert(clrFromVal+'  '+clrToVal);
				if(parseInt(clrFromVal)>parseInt(clrToVal)){
					alert ("Please select properly.");
				}
			}
		}
		catch(err){}
	});
}

//#######################################################################//

function checkColot(){ 
	$(document).ready(function(){
		try{
			colrFromVal = jstrim($('#fromcolor').val());
			colrToVal   = jstrim($('#tocolor').val());
			
			if(colrFromVal!='' && colrToVal!=''){
				//alert(colrFromVal+'  '+colrToVal);
				if(parseInt(colrFromVal)>parseInt(colrToVal)){
					alert ("Please select properly.");
				}
			}
		}
		catch(err){}
	});
}

//#######################################################################//

function setSpinnerValue(ospinnerid,odirection){
	minval=18;
	maxval=60;
	odirection=(odirection!='' && (odirection=='up' || odirection=='down'))?odirection:'up';
	spinnerObj=document.getElementById(ospinnerid);
	ospval=(spinnerObj.value=='' || isNaN(spinnerObj.value))?0:spinnerObj.value;
	if(odirection=='up'){
		if(ospval<maxval){
			ospval++;
		}
		else{
			ospval=ospval;
		}
	}
	else{
		if(ospval>minval){
			ospval--;
		}
		else{
			ospval=ospval;
		}
	}
	spinnerObj.value=ospval;
}

//#######################################################################//

function disableContextMenu(elemrnt) {
    elemrnt.oncontextmenu = function() {
       return false;
    }
}

//#######################################################################//

function swapAdminMenuIcons(tableId, iconId, act){
	 $(document).ready(function(){
		var wbRoot = getDomain();
		
		if(act=='over'){
			oImg     = 'icon_'+iconId+'_hover.jpg';
			addClass = 'menutablehover';
			remClass = 'menutable';
			bgColor	 = '#F1E8E6';
			bgImage	 = wbRoot+'images/admin_images/icons/'+oImg;
		}
		else{
			oImg     = 'icon_'+iconId+'.jpg';
			addClass = 'menutable';
			remClass = 'menutablehover';
			bgColor	 = '#FFFFFF';
			bgImage	 = wbRoot+'images/admin_images/icons/'+oImg;
		}
		//alert(bgImage);
		$('#'+tableId).removeClass(remClass);
		$('#'+tableId).addClass(addClass);
		$('#'+tableId).css('cursor', 'pointer');
		$('#'+tableId).css('background', bgColor+' url('+bgImage+') no-repeat top center');
	});
}

//#######################################################################//

function showCharCount(txtId, countId){
	$(document).ready(function(){
		txtLen = $('#'+txtId).val().length;
		$('#'+countId).html(txtLen);
	});
}

//#######################################################################//
function prepareInputsForHints() {
	var inputs = document.getElementsByTagName("input");
	for (var i=0; i<inputs.length; i++){
		// test to see if the hint span exists first
		if (inputs[i].parentNode.getElementsByTagName("span")[0]) {
			// the span exists!  on focus, show the hint
			inputs[i].onfocus = function () {
				this.parentNode.getElementsByTagName("span")[0].style.display = "inline";
			}
			// when the cursor moves away from the field, hide the hint
			inputs[i].onblur = function () {
				this.parentNode.getElementsByTagName("span")[0].style.display = "none";
			}
		}
	}
	// repeat the same tests as above for selects
	var selects = document.getElementsByTagName("select");
	for (var k=0; k<selects.length; k++){
		if (selects[k].parentNode.getElementsByTagName("span")[0]) {
			selects[k].onfocus = function () {
				this.parentNode.getElementsByTagName("span")[0].style.display = "inline";
			}
			selects[k].onblur = function () {
				this.parentNode.getElementsByTagName("span")[0].style.display = "none";
			}
		}
	}
	
	var textarea = document.getElementsByTagName("textarea");
	for (var j=0; j<textarea.length; j++){
		// test to see if the hint span exists first
		if (textarea[j].parentNode.getElementsByTagName("span")[0]) {
			// the span exists!  on focus, show the hint
			textarea[j].onfocus = function () {
				this.parentNode.getElementsByTagName("span")[0].style.display = "inline";
			}
			// when the cursor moves away from the field, hide the hint
			textarea[j].onblur = function () {
				this.parentNode.getElementsByTagName("span")[0].style.display = "none";
			}
		}
	}
}

function clearText(thefield) {
  if (thefield.defaultValue==jstrim(thefield.value)) { thefield.value = "" }
}

function replaceText(thefield) {
  if (jstrim(thefield.value)=="") { thefield.value = thefield.defaultValue }
}

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			if (oldonload) { oldonload(); }
			func();
		}
	}
}

/*
jQuery(document).ready(function () {
  jQuery('.clearme').focus(function() {
    jQuery(this).val("");
  });
});
*/

//#######################################################################//
