// @author Ashutosh K Tripathi
// @iparam 1)basic text to put in element name and id 2)image name in question 3) Alt text for Image in question 
// @oparam void
// creates an file type input element inside given container id and creates hidden input type to store changed Image name and Alt // text
function ChangeImg(name,ImgName)
{
  var elem = document.getElementById(ImgName);
  elem.innerHTML = "<input type='file' name='"+name+"' class='basicform' id='"+name+"_"+name+"'>";
  var tbodyElem = document.getElementById('EditImage');
  var trElem, tdElem;
  trElem = tbodyElem.insertRow(tbodyElem.rows.length);
  tdElem = trElem.insertCell(trElem.cells.length);
  tdElem.innerHTML = "<br><input type='hidden' name='Rem"+name+"' value='"+ImgName+"'>";
}

// @author Ashutosh K Tripathi
// @iparam 1)basic text to put in element name and id 2)image name in question 3) Alt text for Image in question
// @oparam void
// removes the Image from container td and creates hidden input type to store deleted Image name and Alt text
function DeleteImg(name,ImgName,ImgAlt)
{
  var elem = document.getElementById("td"+ImgName);
  if(confirm("Are you sure you want to delete this Picture.")){
   elem.innerHTML = "";
  var tbodyElem = document.getElementById('EditImage');
  var trElem, tdElem;
  trElem = tbodyElem.insertRow(tbodyElem.rows.length);
  tdElem = trElem.insertCell(trElem.cells.length);
  tdElem.innerHTML = "<br><input type='hidden' name='Rem"+name+"' value='"+ImgName+"'><input type='hidden' name='Rem"+name+"Alt' value='"+ImgAlt+"'>";
   if(document.forms[0].UserPicsCount)
   {
     document.forms[0].UserPicsCount.value = parseInt(document.forms[0].UserPicsCount.value) - 1 ;
   }
   if(document.forms[0].PropPicsCount)
   {
     document.forms[0].PropPicsCount.value = parseInt(document.forms[0].PropPicsCount.value) - 1 ;
   }
  }
}

// @author Ashutosh K Tripathi
// @iparam 1)element id to populate html 2)element name to assign to newly created element 
// @oparam void
// creates an file type input element inside given container id
function ChangeImage(divID,elemName)
{
  var elem = document.getElementById(divID);
  elem.innerHTML = "<input type='file' class='softform' name='"+elemName+"' id='req_"+elemName+"_"+elemName+"' size='36'>";
}

// @author Ashutosh K Tripathi
// @iparam 1)element id to populate html 2)element name to assign to newly created element 
// @oparam void
// add one file type element to given div element to realise 'Add More' function for Image
function ImgAddMore(divID,elemName)
{
  var tbodyElem = document.getElementById(divID);
  var trElem, tdElem;
  var elemErr = document.getElementById("ShowError"); 
 if(document.forms[0].UserPicsCount)
  {
	if(document.forms[0].UserPicsCount.value >= 1){
		elemErr.innerHTML="[ You can upload only 1 Image ! ]";	 
		alert(" You can upload only 1 Image !");
	}	
	else{
		  	trElem = tbodyElem.insertRow(tbodyElem.rows.length);
  			trElem.className = "tla";
  			tdElem = trElem.insertCell(trElem.cells.length);
  			tdElem.innerHTML = "<br><input type='file' class='rvform' name='"+elemName+"[]' size='60'><br>About Image&nbsp;&nbsp;&nbsp;&nbsp;<input type='text' class='rvform' name='"+elemName+"Alt[]' size='25'>";  
 			 if(document.forms[0].UserPicsCount)
   				document.forms[0].UserPicsCount.value = parseInt(document.forms[0].UserPicsCount.value) + 1 ;
			 
			}
  }
  else if(document.forms[0].PropPicsCount)
  {
	if(document.forms[0].PropPicsCount.value >= 3){
		elemErr.innerHTML="[ You can upload only 3 Images ! ]";
		alert(" You can upload only 3 Images !");
	}	
	else{
		  	trElem = tbodyElem.insertRow(tbodyElem.rows.length);
  			trElem.className = "tla";
  			tdElem = trElem.insertCell(trElem.cells.length);
  			tdElem.innerHTML = "<br><input type='file' class='rvform' name='"+elemName+"[]' size='60'><br>About Image&nbsp;&nbsp;&nbsp;&nbsp;<input type='text' class='rvform' name='"+elemName+"Alt[]' size='25'>";  
 			 if(document.forms[0].PropPicsCount)
   				document.forms[0].PropPicsCount.value = parseInt(document.forms[0].PropPicsCount.value) + 1 ;
			 
			}
  }
  else{
  trElem = tbodyElem.insertRow(tbodyElem.rows.length);
  trElem.className = "tla";
  tdElem = trElem.insertCell(trElem.cells.length);
  tdElem.innerHTML = "<br><input type='file' class='rvform' name='"+elemName+"[]' size='60'><br>About Image&nbsp;&nbsp;&nbsp;&nbsp;<input type='text' class='rvform' name='"+elemName+"Alt[]' size='25'>";  
  if(document.forms[0].UserPicsCount)
   document.forms[0].UserPicsCount.value = document.forms[0].UserPicsCount.value + 1 ;
  }
}

// @author Ashutosh K Tripathi
// @iparam void
// @oparam void
// validates an html form taking 0th element of forms array under document object,
// takes decesion that a field is required by spliting id attribute of element by '_',

function FormValidate(id)
{
 var mf = document.getElementById(id);
 var elemErr = document.getElementById("ShowError");
 var ErrStr = "";
 var focusto ="";
 var flag=true ;
 for(var i=0; i<mf.elements.length; i++)
  {
    var arr = mf.elements[i].id.split("_");
	if(arr[0]=="req")
	{
    switch(mf.elements[i].type) 
    { 
     case "text": 
			if(arr[2]=="email")
		   {            
		    if(!emailCheck(mf.elements[i].value)){
				ErrStr += "  ["+arr[1]+" Not Valid.]  ";
				focusto = i;
				flag=false;}
		   }
		   else if(arr[2]=="url")
		   {
		    if(!urlCheck(mf.elements[i].value)){
				ErrStr += "  ["+arr[1]+" Not Valid.]  ";
				focusto = i;
				flag=false;}
		   }
		   else if(arr[2]=="num")
		   {
		    if(isNAN(mf.elements[i].value)){
				ErrStr += "  ["+arr[1]+" Should Be Numeric.]  ";
				focusto = i;
				flag=false;}
		   }   
			else if(mf.elements[i].value=="")
		   {
	        ErrStr += "  ["+arr[1]+" Not Entered.]  ";
	        if(flag){
				focusto = i;
				flag=false;}
		   }		   
	 break;	 
     case "select-one": 
	 	   if(mf.elements[i].options[mf.elements[i].selectedIndex].value=="")
		   {
	        ErrStr += "  ["+arr[1]+" Not Selected.]  ";
			if(flag){
				focusto = i;
				flag=false;}
	       }
	 break; 
     case "textarea":
           if(mf.elements[i].value=="")
		   {
	        ErrStr += "  ["+arr[1]+" Not Entered.]  ";
			if(flag){
				focusto = i;
				flag=false;}
	       }
	 break;
	 case "password":
           if(mf.elements[i].value=="")
		   {
	        ErrStr += "  ["+arr[1]+" Not Entered.]  ";
			if(flag){
				focusto = i;
				flag=false;}
	       }
	 break;
     case "file":
	       if(mf.elements[i].value=="")
		   {
	        ErrStr += "  ["+arr[1]+" Not Entered.]  ";
			if(flag){
				focusto = i;
				flag=false;}
	       }     
	 break;
	 }
	}
	else if(arr[0]=='num')
	{
	  if(isNAN(mf.elements[i].value)){
				ErrStr += "  ["+arr[1]+" Should Be Numeric.]  ";
				focusto = i;
				flag=false;}
	}
  }
  if(ErrStr!="")
	{
	 elemErr.innerHTML = "Please Check :   "+ErrStr;  
	 if(mf.elements[focusto].type!='hidden')
	 mf.elements[focusto].focus();
	 alert("Please See At The Top Of Form For Incomplete Field Details.")
	 return false;
	}
  return true;
}

// @author Ashutosh K Tripathi
// @iparam html form object as input parameter
// @oparam void
// resets text, select-one, textarea form fields of given form field
function SearchFormReset(mf)
{ 
   for(var i = 0;i < mf.elements.length;i++) 
           { 
   	         var index;
	         switch(mf.elements[i].type) 
             { 
              case "text": 
               mf.elements[i].value = ""; 
                 break; 
              case "select-one": 
				mf.elements[i].selectedIndex=0;
                break; 
              case "textarea":
                mf.elements[i].value=0; 
    			break
             } 
           }// for loop ends  
}// function SearchFormReset Ends

function emailCheck(str) {
// function taken from- http://www.smartwebby.com
		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1)
		   return false
		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr)
		   return false
		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr-1)
		   return false
		if (str.indexOf(" ")!=-1)
		    return false
		return true					
	}
function urlCheck(str) {
// function writen by Ashutosh K Tripathi on the lines of emailCheck above
        var dot="."
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if(lstr<=7)
			return false        
		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr)
		   return false
		if (str.indexOf(" ")!=-1)
		    return false
		if(str.substring(0,7)!='http://')
			return false
		return true					
	}

function createBookmark()
 {// function copied from web
  if (navigator.appName=="Netscape")
  {
   alert(
   'To bookmark this site, click '
  +'<b>Bookmarks | Add bookmark</b> '
  +'or press <b>Ctrl+D</b>.'
  )
 }
 else if (parseInt(navigator.appVersion)>3) {
 if (window.external)
 {
   window.external.AddFavorite(location.href, document.title);
 } 
 }
}

var http ; // global variable for ajax xmlhttp request

//function createRequestObject
// @iparam void
// @oparam xmlhttpRequest Object
//function to actually create global xmlhttpRequest Object 
function createRequestObject()
        {
	      var xmlhttp=false;
	      try 
	       {
	 	    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	       }catch (e) 
	        {try 
		     {
			   xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	  		 }catch (E) 
		      {
		       xmlhttp = false;
	  	      }
	       }
	     if (!xmlhttp && typeof XMLHttpRequest!='undefined') 
	      {
		   try 
		   {
			xmlhttp = new XMLHttpRequest();
	       }catch (e) 
		    {
			 xmlhttp=false;
		    }
	     }
	    if (!xmlhttp && window.createRequest) 
	    {
		try 
		 {
			xmlhttp = window.createRequest();
		 }catch (e) 
		  {
			xmlhttp=false;
		  }
	  }	
	return xmlhttp;
}  //Create Request Object function ends.


function RateNow(Module, id)
{
  var Ratingtd1, Ratingtd2, DoRatingtd1, DoRatingtd2 ;
  var DoRating = document.getElementById('DoRating') ;
  var Rating = DoRating.options[DoRating.selectedIndex].value ;
  http=createRequestObject();
  http.onreadystatechange = function(){RateNowResponse();};
  http.open("GET", "asphp/RatingProcess.php?purpose="+Module+"&id="+id+"&tr="+Rating, true);
  http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
  http.send(null);
  
  Ratingtd1 = document.getElementById('Ratingtd1') ;
  Ratingtd2 = document.getElementById('Ratingtd2') ;
  DoRatingtd1 = document.getElementById('DoRatingtd1') ;
  DoRatingtd2 = document.getElementById('DoRatingtd2') ;  
  Ratingtd1.innerHTML = "<img src='web_images/loading.gif'>" ;
  Ratingtd2.innerHTML = "<img src='web_images/loading.gif'>" ;
  DoRatingtd1.innerHTML = "" ;
  DoRatingtd2.innerHTML = "" ;
}

function RateNowResponse()
{
 if(http.readyState==4)
  {	  
   if(http.status==200)
   {
     var Ratingtd1, Ratingtd2 ;
	 var Response = http.responseText ;
	 Ratingtd1 = document.getElementById('Ratingtd1') ;
     Ratingtd2 = document.getElementById('Ratingtd2') ;     
	 if(Response!=1 && Response!=0)
	  {
		  Ratingtd1.innerHTML = Response ;
		  Ratingtd2.innerHTML = Response ;
	  }
   }
 }
}