// ################################################## //
// ===============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_au/':'/new_mpt_au/';
   	   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_uk/tutorial-centers/':'/new_mpt_uk/tutorial-centers/';
   	   //var rootDir = (jstrim(window.location.hostname).toLowerCase()=='localhost')?'/new_mpt_uk/tutorial-centers/':'/tutorial-centers/';
	   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);
	});
}

