// JavaScript Document
// JavaScript Document
var allMsgDisplayId='';
var http = getHTTPObject();
var possibleFormElements=new Array("input","select","textarea","file");

 function startUpload(){
	 	if(document.getElementById('myfile').value!=''){
		  document.getElementById('err_upload_process').style.display='none';
		  document.getElementById('f1_upload_process').style.visibility = 'visible';		  
		  return true;
		}
		else{
			document.getElementById('err_upload_process').style.display='block';
			}
      }

      function stopUpload(success){

      var result = '';

		if (success == 1){
			result='<span class="successMsg">The logo was uploaded successfully!</span>';
		}
		else if(success == 2){
			result='<span class="errorMessage">Invalid file type / extension!';
			}
		else if(success == 3){
			result='<span class="errorMessage">Invalid file size!';
			}
			
		else {
			'<span class="errorMessage">There was an error during file upload!<\/span><br/><br/>';

      }
		document.getElementById('result').innerHTML =result;
      document.getElementById('f1_upload_process').style.visibility = 'hidden';

      return true;

      }
	  
function chg2Upper(idFrmVerify){
	document.getElementById(idFrmVerify).value=document.getElementById(idFrmVerify).value.toUpperCase();	
	}
	
function chkContactForm(){
	var verifyChar=chkContactForm.arguments[0];
	var err='';
	if(strippedValues('contactName')==0)
		err+="<br /> Your name";
	/*if(strippedValues(document.getElementById('contactAddress').value)==0)
		err+="\n Your Address"
	if(strippedValues(document.getElementById('contactCity').value)==0)
		err+="\n Your City"	*/
	
	if(checkEmail(document.getElementById('contactEmail').value)==0)
		err+="<br /> Invalid Email";	
	if(document.getElementById('contactQuery').value=='')
		err+="<br /> Your query / comments";
	if(document.getElementById('contactCapcha').value!=verifyChar)
		err+="<br /> Invalid character";		
	if(allErrorDisplay(err,"showContactErrMsg")){
		allMsgDisplayId="mainBodyContent";
		url="ajaxPages/formProcessing.php?formName=contact&mathRandomNum="+Math.random();	
		callAjaxFxnToPostData('frmContact',url);	
		}	
	else{
		return false;
		}
	
	}


function readMore(){
	cid=readMore.arguments[0];
	allMsgDisplayId="mainBodyContent";
	var url = "ajaxPages/readMoreComment.php?&cid="+cid+"&mathRandom="+Math.random();	
	callAjaxFxn(url);
	}
function showHideIds(showId, HideId){
	document.getElementById(showId).style.display='block';
	document.getElementById(HideId).style.display='none';
	}
function enableob(o) { eval(o+".disabled = false"); }
function disableob(o) { eval(o+".disabled = true"); }
function enableob2(o) { eval(o+".disabled = true"); }
function disableob2(o) { eval(o+".disabled = false"); }

function validateRadioButton(radioChoices){	

	var checkFlag=-1;	
	for(var i=0; i<radioChoices.length;i++){
        if(radioChoices[i].checked){
			checkFlag=i;
			break;
		}//end of for loop	
    }	
	return checkFlag;
	
}
function validateDonation()
{
	var err='';		
	if(document.getElementById('full_name').value == ""){
		err+="<br/>Your full name";
	}
	if(document.donate.address.value == ""){
		err+="<br/>Your address";
	}
	if(document.getElementById('txtEmail').value != "" && checkEmail(document.getElementById('txtEmail').value)==0){
		err+="<br/>Invalid email";
	}
	if(document.donate.country.value == "0"){
		err+="<br/>Choose country";
	}
	if(validateRadioButton(document.donate.type_of_donation)==-1){		
		err+="<br/>How do you want to donate";
	}
	if(document.donate.type_of_donation[1].checked && document.donate.organization.value == ""){
			err+="<br/>Organization / Company name";
	}
	/*if(document.donate.type_of_donation[1].checked && document.donate.orgPhoto.value == ""){
			err+="<br/>Organization logo";
	}*/
	
	if(validateRadioButton(document.donate.fund_provided)==-1){
		err+="<br/>What can you donate";
	}
	if(document.donate.fund_provided[0].checked && (isNaN(document.donate.amount.value) || document.donate.amount.value=='')){
			err+="<br />Invalid amount value";
	}
	if(document.donate.fund_provided[1].checked && document.donate.other_description.value==''){		
			err+="<br/>Other Materials";
		}
	if(document.donate.fund_provided[2].checked && (isNaN(document.donate.amount.value) || document.donate.amount.value=='') && document.donate.other_description.value==''){
		err+="<br/>Invalid amount value<br />Other materials";
	}	
	if(allErrorDisplay(err,"idDonation")){
		allMsgDisplayId="showDonate";
		var url = "ajaxPages/donationProcess.php?mathRandom="+Math.random();	
		callAjaxFxnToPostData("donate",url);
		}
	
}


function ValidateRefer(randomChar){
	var err='';	
	var name=document.getElementById('name').value;
	var fname=document.getElementById('fname').value;
	var senderEmail=document.getElementById('email').value;
	var receiverEmail=document.getElementById('femail').value;	
	var message=document.getElementById('message').value;	
	if(strippedValues('name')=='')
		err+="<br/>Your name";
	if (checkEmail(senderEmail)==0)
		err+="<br/>Your email";
	if(strippedValues('fname')=='')
		err+="<br/>Friend's Name";
	if(checkEmail(receiverEmail)==0)
		err+="<br/>Friend's email";	
	if(message=='')
		err+="<br/>Your message";	
	if(randomChar!=document.getElementById('txtverifyWordFieldRefer').value)
		err+="<br/>Inavlid character";	
	if(allErrorDisplay(err,"idReferShowMsg")){
		allMsgDisplayId="showrefer";
		var url = "ajaxPages/referProcess.php?name="+name+"&senderEmail="+senderEmail+"&fname="+fname+"&receiverEmail="+receiverEmail+"&message="+message+"&mathRandom="+Math.random();	
		callAjaxFxn(url);
		}

 }


function checkEmail(){
	var strEmail, strError, countAtRate, countDot, i;
	var checkAtRate, checkDot;
	var ValidChars,CountValidChars;
	ValidChars="abcdefghijklmnopqrstuvwxyz0123456789_.@ABCDEFGHIJKLMNOPQRSTUVWXYZ-";
	strEmail = checkEmail.arguments[0];
	countAtRate=0;
	countDot=0;
	CountValidChars=0;
	if (strEmail.length > 7){
		for(i=0;i<strEmail.length;i++){
			if(strEmail.charAt(i)=="@")
				countAtRate++;
			if(strEmail.charAt(i)==".")
				countDot++;
			CountValidChars=0;
			for(j=0;j<ValidChars.length;j++){
				if(strEmail.charAt(i)==ValidChars.charAt(j)){
					CountValidChars++;
					}
				}//end of j-for loop
			if(CountValidChars==0){
				strError=0;
				break;
				}
			}//end if i-for loop
		}
	checkAtRate=strEmail.indexOf("@",1);
	checkDot=strEmail.indexOf(".",1);
	for(i=1;i<countDot;i++)
		checkDot=strEmail.indexOf(".",checkDot+1);
	if(countAtRate==1 && countDot > 0 && strEmail.length >=7 && strError != 0)
		strError=1;
	else
		strError=0;
	if(checkDot>=strEmail.length-2)
		strError=0;
	if(strEmail.charAt(0)=="@" || strEmail.charAt(strEmail.length-1)=="@")
		strError=0;
	if(strEmail.charAt(0)=="." || strEmail.charAt(strEmail.length-1)==".")
		strError=0;
	if(checkDot < checkAtRate)
		strError=0;
	return strError;
}

function allErrorDisplay(errMsg,idForErr){
	if(errMsg!=''){
		errMsg="Please check the missing fields"+errMsg;
		document.getElementById(idForErr).innerHTML=errMsg+"<br/><br/>";
		location.href="#"+idForErr;		
		return false;
		}
	else
		return true;	
}

function chkcomment(){	
	var err='';	
	var comment=document.getElementById('commentField').value;
	var name=document.getElementById('txtCommenNameField').value;
	var emailAdd=document.getElementById('txtCommentEmailField').value;
	var parentId=document.getElementById('txtHiddenCommentId').value;
	if(parentId=='')
		parentId=0;
	if(strippedValues('commentField')=='')
		err+="<br/>Comment Field";
		
	if(strippedValues('txtCommenNameField')=='')
		err+="<br/>Your Name";
		
	if(checkEmail(emailAdd)==0)
		err+="<br/>Invalid Email";
	if(chkcomment.arguments[0]!=document.getElementById('txtverifyWordField').value)
		err+="<br/>Invalid Character Verification";
	if(allErrorDisplay(err,"showMissingParameter")){
		allMsgDisplayId="showCommentForm";
		var url = "ajaxPages/commentProcess.php?name="+name+"&emailAdd="+emailAdd+"&comment="+comment+"&parentId="+parentId+"&mathRandom="+Math.random();		
		callAjaxFxn(url);
		}
	}
	
	
function callAjaxFxn(url){
	http=null;
	http=getHTTPObject();	
	http.open("GET", url , true)
	http.onreadystatechange = stateChange;	
	http.send(null);
}

function callAjaxFxnToPostData(frmName,url){	
	http=null;
	http = getHTTPObject();
	var postValues='';	
	postValues=getFormElementsAndValues(frmName);	
	http.open("POST", url , true);
	http.onreadystatechange = stateChange;
	http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");	
	http.send(postValues);	
}

function getFormElementsAndValues(form_name){
	var out = new Array();
	var formInputs='';
	var frmElementName='';
	var elementName=''
	var postData='';
	var radioCheck='';
	var radioButton;
	var radioCheckedValue;
	var frmElementType;
	//alert(possibleFormElements.length);
	for(var j=0;j<possibleFormElements.length;j++){
		elementName=possibleFormElements[j];
		//alert(document.getElementById(form_name).innerHTML);
		formInputs = eval('document.'+form_name+'.getElementsByTagName("'+elementName+'")');		
		//alert(formInputs.length+'  '+elementName);
		for (var i = 0; i < formInputs.length; i++){							
				frmElementName=formInputs.item(i).name;						
				frmElementType=formInputs.item(i).type;	
				/*if(frmElementType=='file')
					alert(eval("document.getElementById('"+frmElementName+"').value"));*/
				if(frmElementType=='radio'){
			radioButton=eval("document."+form_name+"."+frmElementName);
radioCheckedValue=validateRadioButton(radioButton);						
					value=eval("document."+form_name+"."+frmElementName+"["+radioCheckedValue+"].value");

					}				
				else if(frmElementType=='checkbox'){					
					if(eval("document.getElementById('"+frmElementName+"').checked"))
						value=value=eval("document.getElementById('"+frmElementName+"').value");
				}
				/*alert("type="+frmElementType);
				alert("Name="+frmElementName);
				value=eval("document.getElementById('"+frmElementName+"').value");
				alert("value="+value);*/
				if(frmElementType!='radio' && frmElementType!='checkbox'){					
					value=eval("document.getElementById('"+frmElementName+"').value");
				}				
				
				var subBtn=frmElementName.toLowerCase();
				var btnBtn=frmElementName.toLowerCase();
				
				if(subBtn!='submit' || btnBtn!='button'){					
					if(postData=='')
						postData=frmElementName+"="+value;
					else{
						var exitsOrNot=eval("postData.indexOf('"+frmElementName+"=')");						
						if(frmElementType=='radio'){							
							
							radioCheck=eval("document."+form_name+"."+frmElementName+"["+radioCheckedValue+"].checked");							
						}
						else{
							radioCheck=true;
						}
						if(exitsOrNot==-1 && radioCheck)
							postData+="&"+frmElementName+"="+value;
						}
				
				}
		//alert(postData);
		}//enf of i-for*/
	}//end of j-for		
	return (postData);
}

function stateChange(){
  		if( http.readyState == 4){
			var results = http.responseText			
			document.getElementById(allMsgDisplayId).innerHTML=http.responseText;			
		}
		else{
			document.getElementById(allMsgDisplayId).innerHTML="Processing....";
			}
	}
function getHTTPObject() {
	var xmlhttp=null;
 	if(window.XMLHttpRequest){
    	xmlhttp = new XMLHttpRequest();
  	}
  	else if (window.ActiveXObject){
    	xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    	if (!xmlhttp){
        	xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
    	}
    }
  	return xmlhttp;
}

function strippedValues(id){  						
	var returnText=document.getElementById(id).value;
	var returnTextFinal=returnText.replace(/^\s+|\s+/,"");	
	return returnTextFinal;
}

