// JavaScript Document

function ArtCounture(){
	
	this.hightLightMenu=function(Obj,index){
		getTd=$(Obj).getElementsByTagName("td")
		if(index!=-1){
		getTd[index].className="highlightLM";
		}
	}
	
	
			var ResponsWritePlace;
		var LoadedContent;

		this.CreateAJAXRequest=function(url,rPlace,pars,lContent){
		ResponsWritePlace=rPlace;
		LoadedContent=lContent;
		pars=pars+"&killCache="+new Date().getTime();
		ajaxURL=url;
		if($('loadingDIV')){
			Element.show("loadingDIV")
		}

		var myAjax = new Ajax.Request(
			url, 
			{
				method: 'get', 
				parameters: pars, 
				onSuccess: CallSucess,
				onFailure:CallError,
				onComplete: showResponse
			}
		);
	}
	
	function CallSucess(){
		//alert('success')
	}
		
	function CallError(){
		alert('Your Request Failed \n --------  '+ajaxURL);
	}
	
///lightbox hiding 
	this.hideLightBoxOuterControl=function(){
		if(browser=="Internet Explorer"){
			commonUseLightBox1= new lightbox(); //for hideSelects function
			commonUseLightBox1.hideSelects('visible');
			commonUseLightBox1.setScroll(0,getCurrentScrollY);
			commonUseLightBox1.prepareIE('auto', 'hidden','auto');
		}
			
			if($('lbContent')){
				Element.remove($('lbContent'));
			}
			Element.hide($('overlay'));
			Element.hide($('lightbox'));
	}
	
	this.ShowLightBoxOuterControl=function(){
		if(browser=="Internet Explorer"){
			commonUseLightBox1= new lightbox(); //for hideSelects function
			commonUseLightBox1.hideSelects('hidden');
			commonUseLightBox1.getScroll();
			commonUseLightBox1.prepareIE('100%', 'hidden','hidden');
			commonUseLightBox1.setScroll(0,0);
		}
			
			Element.show($('overlay'));
			Element.show($('lightbox'));
	}
	
	this.ChangeLBPosSize=function(type){
	
		
		
			if(type==1){
				$('lightbox').style.width='850px'
				$('lightbox').style.height='500px'
				$('lightbox').style.left="32%"
				$('lightbox').style.top="40%"
	
				}
			else if(type==2){
				$('lightbox').style.width='550px'
				$('lightbox').style.height='500px'
				$('lightbox').style.left='45%'
				$('lightbox').style.top='39%'
				}
			else if(type==3){
				$('lightbox').style.width='550px'
				$('lightbox').style.height='500px'
				$('lightbox').style.left='45%'
				$('lightbox').style.top='45%'
				}
		
		}
		
		
	function showResponse(originalRequest){
		
		//this checking it is normal MASTER PAGES Request or lightbox submition? 
		//First one is normal MASTER PAGES AJAX Request
			
			///remove already available(prevoius loaded) element
		if(ResponsWritePlace!="noPrintRequired" || LoadedContent!="NoContent"){
			
			///remove already available(prevoius loaded) element
			if($(LoadedContent)){Element.remove($(LoadedContent));}
			
				//put returned XML in the Container
				info = "<div id="+LoadedContent+">" + originalRequest.responseText + "</div>";
				
				new Insertion.Before($(ResponsWritePlace), info);//this enable javascript in loaded page
				
				if(LoadedContent=="lbContent"){//problem for FireFox 
					$('lightbox').className = "done";
				}
				if($('loadingDIV')){
					Element.hide("loadingDIV")
				}

		}//if end
		else{
					if($('loadingDIV')){
						Element.hide("loadingDIV")
					}

				
				//$('responseText').value=originalRequest.responseText;
				//alert(originalRequest.responseText);
				//return false;
				//for avoiding responsetext's string conversion pblm that I stored into hidden TEXT Box
				var cutResponse= new Array();

				if($('responseText')){//if obj available
					$('responseText').value=originalRequest.responseText;
					cutResponse=$('responseText').value.split('+++');
					$('responseText').value=cutResponse[0];
				}
				
				///---------------------Login--------------------------------------
				if(Trim(cutResponse[0])=="invalidUserName"){
					Form.enable("loginForm");
					$(cutResponse[1]).focus();
					$(cutResponse[1]).className='redBorder';
					$(cutResponse[3]).className='redBorder';
					$('loginMessage').style.display="block";
					$('loginMessageWriterTd').innerHTML='<span> <img src="../images/icons/exclamation.gif" width="16" height="16" align="absmiddle"  /> '+cutResponse[2]+' </span>'
				}
				else if(Trim(cutResponse[0])=="loginSucess"){
					$(cutResponse[1]).style.display="none";
					location.href=cutResponse[2];
				}
				else if(Trim(cutResponse[0])=="duplicationOccure"){
					Form.enable(cutResponse[3]);
					$('messageBox').style.display="block";
					$('messageWriterTD').innerHTML='<span> <img src="../images/icons/exclamation.gif" width="16" height="16" align="absmiddle"  /> '+cutResponse[2]+' </span>'
				}
				else if(Trim(cutResponse[0])=="proceedSubmition"){ // for normal submission ////
					Form.enable("artistForm");
					eval(cutResponse[2]);				//artCoutureClass.validateForm('artistForm','artistName##text,artistEmail##email,artistPhone##num,artistPhoto##File','do_all_actions.php','createArtist','','post','')			
				}
				//////for categoryTYpeManager, 
				else if(Trim(cutResponse[0])=="commonRedirect"){
					//$(cutResponse[1]).style.display="none";
					location.href=cutResponse[2];
				}
				
				else if(Trim(cutResponse[0])=="afterSentBuyReq"){
					Effect.BlindUp("sendRequestDIV");
					$('afterMSendWriter').innerHTML="<span class='messageBox'> <img src='../images/icons/accept.gif' align='absmiddle' /> "+cutResponse[2]+"</span>"
				}


			}//end of ELSE
}
	
	
			//////////////////Validating form
///Form Id, all fields to sent, Server Side Script File URL, action to do(edit or create), Message Table Id, Submit type (AJAX or Normal), Duplication check Via AJAX(because some form required normal submission from LightBox, before that we need to check duplication
			this.validateForm=function(formObj,fields,URL,action,messageTable,submitType,validateDuplication){
				
			if($('messageBox')){
				$('messageBox').style.display="none";
			}

			cutFieldParams=fields.split(',');
			
			compareFieldBool=false;compareFieldFirstVal="";ErrorCount=0;
			
			//reseting
			for(i=0;i<cutFieldParams.length;i++){
				getValidType=cutFieldParams[i].split("##");
				eval("document."+formObj+"."+getValidType[0]).className="";
			}
			
			for(i=0;i<cutFieldParams.length;i++){
				getValidType=cutFieldParams[i].split("##");
				if(getValidType[1]=="text"){//blank as text
					if(Trim(eval("document."+formObj+"."+getValidType[0]+".value"))==""){
						eval("document."+formObj+"."+getValidType[0]).className="redBorder";
							if($(getValidType[0]+'Label')){
								$(getValidType[0]+'Label').innerHTML="Should not be blank";
								$(getValidType[0]+'Label').className="mandatoryStar";
							}
						ErrorCount++;
					}
					else{
							if($(getValidType[0]+'Label')){
								$(getValidType[0]+'Label').innerHTML="";
								$(getValidType[0]+'Label').className="grayTextForLabel";
							}
					}
					if(getValidType[0]=="uname" && eval("document."+formObj+"."+getValidType[0]+".value")!=""){
						var illegalChars = /\W/;
						if (eval("document."+formObj+"."+getValidType[0]+".value").length < 4) {
							eval("document."+formObj+"."+getValidType[0]).className="redBorder";
							if($(getValidType[0]+'Label')){
								$(getValidType[0]+'Label').innerHTML="Minimum 4 character required";
								$(getValidType[0]+'Label').className="mandatoryStar";
							}
							ErrorCount++;
						} else if (illegalChars.test(eval("document."+formObj+"."+getValidType[0]+".value")))  {
							eval("document."+formObj+"."+getValidType[0]).className="redBorder";
							ErrorCount++;
						}
					}
				}
				else if(getValidType[1]=="compare"){//compare password
					if(eval("document."+formObj+"."+getValidType[0]+".value")==""){
						eval("document."+formObj+"."+getValidType[0]).className="redBorder";
						if($(getValidType[0]+'Label')){
							$(getValidType[0]+'Label').innerHTML="Password required.";
							$(getValidType[0]+'Label').className="mandatoryStar";
						}
						ErrorCount++;
					}
					else if(eval("document."+formObj+"."+getValidType[0]+".value").length<6){
						eval("document."+formObj+"."+getValidType[0]).className="redBorder";
						if($(getValidType[0]+'Label')){
							$(getValidType[0]+'Label').innerHTML="Password should be Minimum 6 charecters.";
							$(getValidType[0]+'Label').className="mandatoryStar";
						}
						ErrorCount++;
						}
					else if(eval("document."+formObj+".password.value")!=eval("document."+formObj+".confirmPassword.value")){
						eval("document."+formObj+"."+getValidType[0]).className="redBorder";
						if($(getValidType[0]+'Label')){
							$(getValidType[0]+'Label').innerHTML="Password mismatch";
							$(getValidType[0]+'Label').className="mandatoryStar";
						}
						ErrorCount++;
					}
					else{
						if($(getValidType[0]+'Label')){
							$(getValidType[0]+'Label').innerHTML="";
							$(getValidType[0]+'Label').className="grayTextForLabel";
						}
					}
					
				}
				else if(getValidType[1]=="email"){//email validation
					
					if(eval("document."+formObj+"."+getValidType[0]+".value")==""){
						eval("document."+formObj+"."+getValidType[0]).className="redBorder";
						if($(getValidType[0]+'Label')){
								$(getValidType[0]+'Label').innerHTML="Email ID required";
								$(getValidType[0]+'Label').className="mandatoryStar";
						}
						ErrorCount++;
					}
					else if(!emailValidation(eval("document."+formObj+"."+getValidType[0]+".value"))){
						eval("document."+formObj+"."+getValidType[0]).className="redBorder";
						if($(getValidType[0]+'Label')){
								$(getValidType[0]+'Label').innerHTML="Valid Email ID required";
								$(getValidType[0]+'Label').className="mandatoryStar";
						}
						ErrorCount++;
					}
					else{
						if($(getValidType[0]+'Label')){
								$(getValidType[0]+'Label').innerHTML="";
								$(getValidType[0]+'Label').className="grayTextForLabel";
						}
					}
				}
				else if(getValidType[1]=="select"){//select box validation
					if(eval("document."+formObj+"."+getValidType[0]+".selectedIndex")==0){
						eval("document."+formObj+"."+getValidType[0]).className="redBorderSelect";
							if($(getValidType[0]+'Label')){
								$(getValidType[0]+'Label').innerHTML="Select from dropdown";
								$(getValidType[0]+'Label').className="mandatoryStar";
							}
						ErrorCount++;
					}
					else{
							if($(getValidType[0]+'Label')){
								$(getValidType[0]+'Label').innerHTML="";
								$(getValidType[0]+'Label').className="grayTextForLabel";
							}						
					}
				}
				else if(getValidType[1]=="num"){//numerical validation
					if(eval("document."+formObj+"."+getValidType[0]+".value")==""){
						eval("document."+formObj+"."+getValidType[0]).className="redBorder";
							if($(getValidType[0]+'Label')){
								$(getValidType[0]+'Label').innerHTML="Should not be blank";
								$(getValidType[0]+'Label').className="mandatoryStar";
							}
						ErrorCount++;
					}
					else if(isNaN(eval("document."+formObj+"."+getValidType[0]+".value"))){
						eval("document."+formObj+"."+getValidType[0]).className="redBorder";
							if($(getValidType[0]+'Label')){
								$(getValidType[0]+'Label').innerHTML="Should be numerical value";
								$(getValidType[0]+'Label').className="mandatoryStar";
							}
						ErrorCount++;
					}//else if
					else{
							if($(getValidType[0]+'Label')){
								$(getValidType[0]+'Label').innerHTML="";
								$(getValidType[0]+'Label').className="grayTextForLabel";
							}						
					}
				}//else if num
				else if(getValidType[1]=="notblankEmail"){//numerical validation
					if(eval("document."+formObj+"."+getValidType[0]+".value")!=""){
						if(!emailValidation(eval("document."+formObj+"."+getValidType[0]+".value"))){
							eval("document."+formObj+"."+getValidType[0]).className="redBorder";
							ErrorCount++;
						}
					}
				}//else if not blank email
				else if(getValidType[1]=="File"){//FILE
					if(eval("document."+formObj+"."+getValidType[0]+".value")==""){
						eval("document."+formObj+"."+getValidType[0]).className="redBorder";
							if($(getValidType[0]+'Label')){
								$(getValidType[0]+'Label').innerHTML="Should not be blank";
								$(getValidType[0]+'Label').className="mandatoryStar";
							}
						ErrorCount++;
					}
					else{
						
						if(!checkExtension(eval("document."+formObj+"."+getValidType[0]))){
							eval("document."+formObj+"."+getValidType[0]).className="redBorder";
							if($(getValidType[0]+'Label')){
								$(getValidType[0]+'Label').innerHTML="Accept only '"+ $(getValidType[0]).accept +"' ";
								$(getValidType[0]+'Label').className="mandatoryStar";
							}
							ErrorCount++;
						}
						else{
							if($(getValidType[0]+'Label')){
								$(getValidType[0]+'Label').innerHTML="";
								$(getValidType[0]+'Label').className="grayTextForLabel";
							}
						}
						
					}
				}
				else if(getValidType[1]=="notblankFile"){//FILE extension validation if not mandatory
					if(eval("document."+formObj+"."+getValidType[0]+".value")!=""){
						if(!checkExtension(eval("document."+formObj+"."+getValidType[0]))){
							eval("document."+formObj+"."+getValidType[0]).className="redBorder";
							if($(getValidType[0]+'Label')){
								$(getValidType[0]+'Label').innerHTML="Accept only '"+ $(getValidType[0]).accept +"' ";
								$(getValidType[0]+'Label').className="mandatoryStar";
							}
							ErrorCount++;
						}
					}
				}

			}///for
			
			if(ErrorCount>0){
				if(messageTable=="loginMessage" ){
					
					Effect.Appear("loginMessage");
					$('loginMessageWriterTd').innerHTML='<span><img src="../images/icons/exclamation.gif" width="16" height="16" align="absmiddle"> Username / Password missing</span>'
				}
				else if(messageTable=="fPErrorMessage"){
					Effect.Appear("fPErrorMessage");
					$('fPErrorMessageWriter').innerHTML='<span><img src="../images/icons/exclamation.gif" width="16" height="16" align="absmiddle"> E-mail Id field missing</span>'
					}
				else if($('messageBox')){
					Effect.Appear("messageBox");
					$('messageWriterTD').innerHTML='<span> <img src="../images/icons/exclamation.gif" width="16" height="16" align="absmiddle"  /> Error! Please fill all mandatory fields marked as <span class="mandatoryStar" >*<span> </span>'
				}
		
				
			}//if
			else{
				
				
				if(messageTable=="loginMessage" ){
					$('loginMessage').style.display="none";
				}
				else if(messageTable=="fPErrorMessage"){
					$('fPErrorMessage').style.display="none";
					}
				else if($('messageBox')){
					$('messageBox').style.display="none";
				}
				else if($('refermessageBox')){
					$('refermessageBox').style.display="none";
				}
				
				//alert(formObj)
				Form.disable(formObj);
				
				ObjToClose="";
				if (submitType=="post"){
					
					Form.enable(formObj);
					$(formObj).action=URL+"?action="+action;
					$(formObj).submit();	
					Form.disable(formObj);
				}
				else{
					if(validateDuplication!=""){ //mean there have some action after Validation
						artCoutureClass.CreateAJAXRequest(URL,'noPrintRequired','action='+action+'&ObjIdToClose='+ObjToClose+'&subaction='+validateDuplication+'&'+Form.serialize(formObj),'NoContent');
					}
					else{
						artCoutureClass.CreateAJAXRequest(URL,'noPrintRequired','action='+action+'&ObjIdToClose='+ObjToClose+'&'+Form.serialize(formObj),'NoContent');
					}
				}
			}//else
		}//Validate function
		
		function emailValidation(ObjValue){
				cutEmailAt=ObjValue.split("@");
				cutEmailDot=ObjValue.split(".");
				if(cutEmailAt.length<2 || cutEmailAt.length>2 ){
					return false;
				}
				else if(cutEmailDot.length<2){
					return false;
				}
				else{
					return true; 
				}
		}
		
		//extension checking Added on 20th Nov 2007
		function checkExtension(Obj){
				cutAcceptEXT=Obj.accept.split(",");
				dot=Obj.value.lastIndexOf("."); 
				fileNamelength=Obj.value.length
				var extension = Obj.value.substr(dot+1,fileNamelength); 
				checkRightExtension=false;
				for(j=0;j<cutAcceptEXT.length;j++){
					if (cutAcceptEXT[j]==extension){
						checkRightExtension=true;	
					}
				}
				return checkRightExtension;
		}
		
		function Trim(s) 
		   {
			// Remove leading spaces and carriage returns
			while ((s.substring(0,1) == ' ') || (s.substring(0,1) == '\n') || (s.substring(0,1) == '\r'))
			 { s = s.substring(1,s.length); }
			 
			// Remove trailing spaces and carriage returns
		 while ((s.substring(s.length-1,s.length) == ' ') || (s.substring(s.length-1,s.length) == '\n') || (s.substring(s.length-1,s.length) == '\r'))
			 { s = s.substring(0,s.length-1); }
			 
			 
			return s;
		   }
		   
		   
		//textarea Character Counter
		this.textareaCounter=function(textAreaObj, count, limiterPrintObj) {
			var text = $(textAreaObj).value;
			var len = text.length;
			if(len > count){
					text = text.substring(0,count);
					$(textAreaObj).value =text;
					return false;
			}
			if($(limiterPrintObj)){
				$(limiterPrintObj).innerHTML=count-len;	
			}
			//document.myform.limit.value = count-len;		
		}
		//textArea Character counter end
		
		//confirm box
		this.confirmMessage=function(URL,action,nametoDisplay,messType){
			message="";
			if(messType=="ct"){
				message="********** WARNING *****************\n\nYour about to delete '"+nametoDisplay+"' \n\nThis will delete ( PERMANENTLY FROM DATABASE ) all ARTS details and Category Details  related to this category type \n\nOr you want only to hide this CATEGORY TYPE form website? Press CANCEL Button and  Just uncheck that enable check box for hiding. \n\n ************************************ ";
				outConf=confirm(message);	
				
			}
			else if(messType=="c"){
				message="********** WARNING *****************\n\nYour about to delete '"+nametoDisplay+"' \n\nThis will delete ( PERMANENTLY FROM DATABASE ) all ARTS details related to this category \n\nOr you want only to hide this CATEGORY form website? Press CANCEL Button and  Just uncheck that enable check box for hiding.\n\n************************************ ";
				outConf=confirm(message);		
				}
			else if(messType=="a"){
				message="********** WARNING *****************\n\nYour about to delete '"+nametoDisplay+"' \n\nThis will delete ( PERMANENTLY FROM DATABASE ) all ARTS details related to this Artist \n\nOr you want only to hide this ARTIST form website? Press CANCEL Button and  Just uncheck that enable check box for hiding.\n\n************************************ ";
				outConf=confirm(message);		
				}

			else{
				outConf=confirm("You are about to "+action+" ' "+nametoDisplay+" ' ");
			}
			if(outConf){
				location.href=URL;
			}
		}
		 
}
artCoutureClass=new ArtCounture;


function MM_validateForm() { //v4.0
  if (document.getElementById){
    var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
    for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=document.getElementById(args[i]);
      if (val) { nm=val.name; if ((val=val.value)!="") {
        if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
          if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
        } else if (test!='R') { num = parseFloat(val);
          if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
          if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
            min=test.substring(8,p); max=test.substring(p+1);
            if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
      } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
    } if (errors) alert('The following error(s) occurred:\n'+errors);
    document.MM_returnValue = (errors == '');
} }
